#630 · Working with data · Intermediate · Mini-project · 165 XP · about 60 min
A bank lets you download your statement as lines of text such as '2026-03-04,Grocery Mart,-45.20': the date, a description and the amount, separated by commas. Money paid out has a minus sign, and money coming in has no sign. Build the tools that turn those lines into a summary. To keep sums exact, every amount is handled as a whole number of cents.
parse_line(line): return (date, description, cents), for example ('2026-03-04', 'Grocery Mart', -4520). Spaces around each part are removed.running_balances(start, transactions): start is the balance in cents before the first transaction, and transactions is a list of tuples from Part 1. Return the list of balances after each transaction.monthly_totals(transactions): a dictionary that maps each month, written 'YYYY-MM', to a pair (money_in, money_out), both as positive cents, with the months in the order they first appear.biggest_spends(transactions, n): the n largest payments out, as (description, cents) tuples with positive cents, largest first. Equal payments keep the order they have in the statement.lowest_point(start, transactions): the date and balance at the moment the balance was lowest, as (date, balance), checked after each transaction. If the lowest balance happens more than once, give the earliest. transactions is never empty here.lines = ['2026-03-01,Salary,2500.00', '2026-03-02,Rent,-1200.00', '2026-03-04,Grocery Mart,-45.20'] t = [parse_line(x) for x in lines] t[2] # ('2026-03-04', 'Grocery Mart', -4520) running_balances(10000, t) # returns [260000, 140000, 135480] monthly_totals(t) # returns {'2026-03': (250000, 124520)} biggest_spends(t, 1) # returns [('Rent', 120000)] lowest_point(10000, t) # returns ('2026-03-04', 135480)
float('19.99') * 100 is 1998.9999999999998 in Python, so int() of it gives the wrong number of cents. Work with the digits of the text instead, or round with care.
First solves only, ranked by stars and then time.