Closed HananRosenthal closed 8 years ago
What kind of help?
i mean, if lambda only gets a word and return its first letter - its stupid
Oh, Why do you feel it stupid?
becuase this can be one code line in the "main" function, without the overhead of a stupid lambda function
This is a concept (of hashing) with a simple example.
What if someone will want to do the same grouping but based on the last character? And then someone else might want to do the grouping with the sum of the ascii values of the strings. You would then need to copy, paste and adjust the grouping function.
With the solution of this exercise the implementor of the grouping function does not need to know about how the hashing algorithm works (first character, last character, sum of ascii, etc.), it can rely on the user supplying the hashing algorithm as the first function.
Let me give you another example: sorting. sorting has two major parts. One is what computer scientist usually call "sorting algorithm". This can be quick-sort, bubble-sort, merge-sort, etc. The other one is what application programmers call the "sorting function" that given two values can decide which one is "bigger" and which one is "smaller". By de-coupling these two functionalities Python can provide a single "sorting algorithm" with a default "sorting function", but you as the user can easily replace the "sorting function" to be "compare the first character of the strings" or "compare the last character of the strings" or "compare the sum of the ascii numbers of the strings".
ok, i got the idea. last question here: how do i add different values (one at a time) to the same key in a dictionary?
For that each value (not the key) in the dict will have to be a list and then you can treat it as a list and append values to it.
hello Gabor,
need some help with 05.py... 10X