The reduce function collapses each key's grouped values into a single output. It's called once per key with the key and its list of values, and returns the aggregate, a sum, a max, a concatenation, whatever the job needs. For word count, reduce sums the ones into a total count.
Build the reduce phase:
reduce_phase(groups, reducer): return a dict mapping each key to reducer(key, values).def reduce_phase(groups, reducer):
"""Collapse each key's values with reducer(key, values)."""
# TODO: return {k: reducer(k, vs) for k, vs in groups.items()}
pass
def wc_reduce(key, values):
"""Word-count reducer: sum the counts."""
return sum(values)
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
The reduce function collapses each key's grouped values into a single output. It's called once per key with the key and its list of values, and returns the aggregate, a sum, a max, a concatenation, whatever the job needs. For word count, reduce sums the ones into a total count.
Build the reduce phase:
reduce_phase(groups, reducer): return a dict mapping each key to reducer(key, values).Press Run Tests or ββ© to check your solution.