ynonp / python-examples-verint-2016-07

Python examples and exercises
MIT License
2 stars 35 forks source link

ToCode Solution: תרגול פונקציות #155

Closed HananRosenthal closed 8 years ago

HananRosenthal commented 8 years ago

hello Gabor,

need some help with 05.py... 10X

szabgab commented 8 years ago

What kind of help?

HananRosenthal commented 8 years ago
  1. what exactly the lamda function must get, and what should it perform
  2. what should the main function perform 10X
HananRosenthal commented 8 years ago

i mean, if lambda only gets a word and return its first letter - its stupid

szabgab commented 8 years ago

Oh, Why do you feel it stupid?

HananRosenthal commented 8 years ago

becuase this can be one code line in the "main" function, without the overhead of a stupid lambda function

szabgab commented 8 years ago

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".

HananRosenthal commented 8 years ago

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?

szabgab commented 8 years ago

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.