thanks everyone, though it was very tough, but i thank GOD On Mon, Dec 28, 2015 at 11:29 PM, Cameron Simpson <c...@zip.com.au> wrote:
> On 28Dec2015 01:34, Prince Udoka <princeud...@gmail.com> wrote: > >> bu i have come up with a solution, that will work but encounter a problem >> in the set, giving set not manipulated correctly: >> >> def manipulate_data(kind, data): >> if kind == 'list': >> return list(data)[::-1] >> elif kind == 'set': >> return set(data) >> elif kind == 'dictionary': >> return dict.keys(data) >> manipulate_data("list", range(1,6)) >> manipulate_data("set", {"a", "b", "c", "d", "e", "ANDELA", "TIA", >> "AFRICA"}) >> manipulate_data("dictionary", {"apples": 23, "oranges": 15, "mangoes": 3, >> "grapes": 45}) >> >> the thing now is the function to use in adding "ANDELA", "TIA", "AFRICA" >> pls 4give my use of language >> > > You are very close. Let me remind you of the original task text: > > add items `"ANDELA"`, `"TIA"` and `"AFRICA"` to the set and return the > resulting set > > Your previous attempt (with hardwired values inside the function) actually > had code to do it. > > While you have pulled out all the hardwired values from the function > (good) and put them in the external calls, note that the task explicitly > says "add items `"ANDELA"`, `"TIA"` and `"AFRICA"` to the set". So _those_ > values _are_ supposed to be hardwired inside the function - they are a > fixed part of the task. So move them back in, as in your previous attempt. > > There is some ambiguity in that part of the question: should you return a > _new_ set consistint of the original set plus the three new values, or > simply add the three values to the original set? Your prior code modified > the original set, which may fit the task specification. > > However, it is a common design objective that functions do not, > _normally_, modify their arguments. So, consider this code: > > set1 = {"a", "b", "c", "d", "e"} > set2 = manipulate_data("set", set1) > > After running this, set2 should look like this: > > {"a", "b", "c", "d", "e", "ANDELA", "TIA", "AFRICA"} > > (in some order -- sets are not ordered). However, what about set1? In your > current code, set1 is modified, so it will be the same. But you can imagine > that it would be more useful for the caller if set1 were unchanged. > > In python, the convention is usually that if a function returns the new > value then it should not modify the original. So you should probably > construct a copy of the original set and modify that: > > data = set(data) > ... add the new values ... > return data > > Cheers, > Cameron Simpson <c...@zip.com.au> > -- https://mail.python.org/mailman/listinfo/python-list