The Reduce Phase

Step 3: The Reduce Phase

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).
Hints
Starter code
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)
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.