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:
Rick van Hattem
2022-09-05 00:04:00 +02:00
parent 82cf71ed1c
commit 500a31afac
34 changed files with 742 additions and 0 deletions
@@ -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()