A chain of market stalls sends its sales to head office as raw CSV text — one long string, straight out of each stall's till. Before anyone can add anything up, that text has to be turned back into rows.
Task: write totals_by(text, key, value). It takes the CSV text, the name of a column to group by (key) and the name of a column of numbers to add up (value). It returns a dictionary mapping each key value to the total of its value column. The order of the keys doesn't matter.
How the text is laid out
"\n", and the first line is always the header row of column names." north " means north.Tills are messy. Skip a line — quietly, without crashing — when:
"\n", which leaves an empty last line),key is empty, or its value is empty or the text NA.Every other value is a valid number, possibly with a decimal point. Keys stay as text, even when they look like numbers.
Example
The NA line is skipped, and so is the empty line after the final "\n".