Editor

Invoice Total

#590 · Working with data · Intermediate · Review the AI's code · 65 XP · about 8 min

Someone asked an AI assistant: "Write a function that totals an invoice. Each line has a unit price and a quantity, and the total should be rounded to 2 decimal places." This is its answer. It looks right, but it has a bug.

invoice_total(lines) takes a list of (unit_price, quantity) pairs. It should add up unit_price * quantity over every line and round the total to 2 decimal places once, at the end. An empty invoice totals 0.

Examples

invoice_total([(2.50, 4), (1.25, 2)])                # returns 12.5
invoice_total([(0.004, 1000)])                       # returns 4.0
invoice_total([(0.004, 1), (0.004, 1), (0.004, 1)])  # returns 0.01
invoice_total([])                                    # returns 0

In the third example the three lines add up to 0.012, which rounds to 0.01.

Constraints

  • lines has 0 to 1,000 pairs; prices are from 0 to 10,000 and quantities are whole numbers from 0 to 10,000.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.