MapReduce starts by transforming input records into key/value pairs. The map function is called once per input and emits zero or more pairs; the framework flattens all of them into a single stream. For word count, mapping the line "a b" emits [("a", 1), ("b", 1)]: one pair per word.
Build the map phase:
map_phase(inputs, mapper): call mapper(item) for each input (each returns a list of pairs) and return all the pairs concatenated in order.def map_phase(inputs, mapper):
"""Apply mapper to each input and flatten the emitted pairs."""
pairs = []
# TODO: walk the inputs left to right and call the mapper on each one. Every
# call hands back a list of pairs, so grow the pairs accumulator with all of
# its entries rather than nesting a list inside it, then return the flat
# accumulator. No inputs means an empty list comes back.
pass
def wc_map(line):
"""Word-count mapper: one (word, 1) pair per word."""
return [(w, 1) for w in line.split()]
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
MapReduce starts by transforming input records into key/value pairs. The map function is called once per input and emits zero or more pairs; the framework flattens all of them into a single stream. For word count, mapping the line "a b" emits [("a", 1), ("b", 1)]: one pair per word.
Build the map phase:
map_phase(inputs, mapper): call mapper(item) for each input (each returns a list of pairs) and return all the pairs concatenated in order.Press Run Tests or ββ© to check your solution.