34 lines
712 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Write a groupby function that isnt affected by sorting.
import collections
def groupby(func, seq):
groups = collections.defaultdict(list)
for item in seq:
groups[func(item)].append(item)
return groups
def main():
# Explicitly defined test data for clarity.
xs = [0, 1, 2, 3, 4, 5, 6, 7]
assert groupby(lambda x: x % 2, xs) == {
0: [0, 2, 4, 6],
1: [1, 3, 5, 7],
}
assert groupby(
lambda x: 'even' if x % 2 == 0 else 'odd',
xs,
) == {'even': [0, 2, 4, 6], 'odd': [1, 3, 5, 7]}
assert groupby(lambda x: x > 5, xs) == {
False: [0, 1, 2, 3, 4, 5],
True: [6, 7],
}
if __name__ == '__main__':
main()