Fix the dollar total from payment log lines
Your task
Your finance job totals every dollar amount that appears in a day's payment logs. The first version used m.group(), which includes the $ sign, so float("$12.50") crashed on the first real amount. The new version reads only the number.
An assistant wrote sum_amount_cents(log_lines). It is in the editor. It has a bug. Find it and fix it.
Input and output:
log_lines: the log lines, as strings.- Return the total of every amount on every line, in cents, as an integer.
Rules the tests check:
- An amount is a
$, then one or more digits from 0 to 9, then optionally a.followed by exactly two digits. - As a regex, an amount is
\$([0-9]+)(\.[0-9]{2})?. - With the optional part, the two digits are the cents. Without it, the cents are 0. So
$12.5counts as 1200 cents, and$1.234counts as 123 cents: only the first two digits after the dot belong to the amount. - Leading zeros are fine:
$007.50is 750 cents. - A
$with no digit right after it is not an amount. In$$7.25, the second$starts the amount. - Amounts never overlap: after an amount, look for the next
$after its last character. - The total must be exact for any number of amounts.
Examples
sum_amount_cents(log_lines: list[str]) -> intExample 1
Input
sum_amount_cents(["paid $12.50 to vendor", "refund $3.00"])Output
1550Example 2
Input
sum_amount_cents(["no amounts here", "total: 42"])Output
0Example 3
Input
sum_amount_cents(["$100 and $0.99"])Output
10099