question 204 Dates Numbers & Regex · medium
Invoice totals to the cent
Wholesale prices in the Online Retail data sometimes have three decimals (0.425 a unit). Totals must be exact: add each invoice's lines up exactly and round only the invoice total, half up, to the cent.
Function
invoice_totals(lines)linesdictionaries with
invoice (text), quantity (int) and price (text like "2.55" or "0.425")Returnsa dictionary from each invoice to its total as text with two decimals, e.g.
{'536365': '15.30'}Rulesinvoices starting with
C are cancellations and are left out; round the total once (half up), not each lineExamples
callinvoice_totals([{'invoice': '536365', 'quantity': 6, 'price': '2.55'}, {'invoice': '536365', 'quantity': 6, 'price': '3.39'}, {'invoice': '536366', 'quantity': 6, 'price': '1.85'}, {'invoice': 'C536379', 'quantity': -1, 'price': '27.50'}])returns{'536365': '35.64', '536366': '11.10'}
callinvoice_totals([{'invoice': '536367', 'quantity': 32, 'price': '0.425'}])returns{'536367': '13.60'}
callinvoice_totals([])returns{}