Added example solutions to several chapters. Feel free to create a pull request with your answers. Also for the chapters that have no solutions yet :)
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
# Write a groupby function that returns lists of results instead of
|
||||
# generators.
|
||||
|
||||
import pprint
|
||||
|
||||
|
||||
def groupby(iterable, key=None):
|
||||
'''
|
||||
Return a dictionary of lists of items grouped by the key function.
|
||||
|
||||
Note that as opposed to the itertools.groupby function, this function
|
||||
does not require the iterable to be sorted.
|
||||
'''
|
||||
if key is None:
|
||||
key = lambda x: x
|
||||
groups = {}
|
||||
for item in iterable:
|
||||
groups.setdefault(key(item), []).append(item)
|
||||
return groups
|
||||
|
||||
|
||||
def main():
|
||||
# Demo data from the itertools docs
|
||||
pprint.pprint(groupby('AAAABBBCCDAABBB'))
|
||||
pprint.pprint(groupby('AAAABBBCCD'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user