#289 · Working with data · Intermediate · 65 XP · about 12 min
A company's sales arrive as (region, month, amount) rows, where month is a string 'YYYY-MM' and amount is a whole number of dollars (negative for a refund). Several rows can share the same region and month.
Write sales_pivot(rows) that returns a nested dictionary in which pivot[region][month] is the total of that region's sales in that month. Like a spreadsheet pivot table, every region gets an entry for every month that appears anywhere in rows, with 0 where the region sold nothing that month.
Order the keys: the regions in alphabetical order, and inside each region the months from earliest to latest.
rows = [ ('North', '2026-02', 300), ('South', '2026-01', 120), ('North', '2026-01', 200), ('North', '2026-02', 50), ] sales_pivot(rows) # returns {'North': {'2026-01': 200, '2026-02': 350}, # 'South': {'2026-01': 120, '2026-02': 0}}
South sold nothing in February, but it still gets a '2026-02' entry of 0.
0 <= len(rows) <= 100000-10000 <= amount <= 10000First solves only, ranked by stars and then time.