Fix the cents parser in a reconciliation check

Review the AI's codeMedium15 min

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. Row i uses index i of 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:
    1. missing when unit_prices or stored_totals has no entry for the row, or either value is exactly "" or "nan".
    2. invalid when either value is not a money amount.
    3. mismatch when 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, no 1. and no 1.000.
  • In cents, 5 is 500, 1.5 is 150 and -1.50 is -150.

Examples

find_total_mismatches(quantities: list[int], unit_prices: list[str], stored_totals: list[str]) -> list[str]
Example 1
Inputfind_total_mismatches([3, 2, 1], ["0.10", "19.99", "5.00"], ["0.30", "39.98", "5.01"])
Output["2=mismatch"]
Example 2
Inputfind_total_mismatches([1, 2], ["1.00", "2.00"], ["", "nan"])
Output["0=missing", "1=missing"]