Fix the cents parser in a reconciliation check
Your task
A data quality check gates your financial export. It recomputes each row's total as quantity times unit price and flags rows that disagree with the stored total. The old version compared floats, so 3 * 0.10 did not equal 0.30 and every missing value looked like a mismatch. The new version compares exact integer cents.
An assistant wrote find_total_mismatches(quantities, unit_prices, stored_totals). It is in the editor. It has a bug. Find it and fix it.
Input and output:
quantities: the quantity on each row, as integers. Rowiuses indexiof all three lists.unit_prices: each row's unit price, as text.stored_totals: each row's stored total, as text.- Return one string per flagged row,
"<row>=<status>", with the row index counting from 0, in row order. Clean rows add nothing.
Rules the tests check:
- There is one row per entry in
quantities. Extra entries in the other two lists are ignored. - The status is the first that applies:
missingwhenunit_pricesorstored_totalshas no entry for the row, or either value is exactly""or"nan".invalidwhen either value is not a money amount.mismatchwhen quantity times the unit price, in cents, differs from the stored total in cents.
- A money amount is an optional
-, then one or more digits 0 to 9, then optionally a.and 1 or 2 more digits. Nothing else counts: no spaces, no+, no.5, no1.and no1.000. - In cents,
5is 500,1.5is 150 and-1.50is -150.
Examples
find_total_mismatches(quantities: list[int], unit_prices: list[str], stored_totals: list[str]) -> list[str]Example 1
Input
find_total_mismatches([3, 2, 1], ["0.10", "19.99", "5.00"], ["0.30", "39.98", "5.01"])Output
["2=mismatch"]Example 2
Input
find_total_mismatches([1, 2], ["1.00", "2.00"], ["", "nan"])Output
["0=missing", "1=missing"]