Fix the revenue check for a five table join
Your task
An AI wrote a 60 line SQL report that joins five tables to compute monthly revenue. It ran without error and returned a plausible number. Before that number goes in a board deck, your team recomputes revenue from the joined rows with a small independent check.
An assistant wrote reconcile_joined_revenue(rows). It is in the editor. It has a bug. Find it and fix it.
Input and output:
rows: the rows the join produced, one string per row, as fields separated by;, such asorder_id=A1;amount_cents=1500. A join repeats an order once for each matching row in the joined tables, so one order can appear on many rows.- Return a list of 3 integers: the number of rows, the number of distinct order ids, and the revenue in cents.
Rules the tests check:
- A field that starts with
order_id=gives the row's order id: the rest of the field. A field that starts withamount_cents=gives its amount. If either field appears twice in a row, the later one wins. Other fields are ignored. Nothing is trimmed: a field with a leading space matches neither. - The amount is an optional
-followed by digits 0 to 9. A missing amount, an empty one or anything else counts as 0. - A row with no order id, or an empty one, counts toward the number of rows only.
- Each order id counts once. The first row with that id adds its amount to the revenue. Later rows with the same id add nothing, even if their amount is different.
Examples
reconcile_joined_revenue(rows: list[str]) -> list[int]Example 1
Input
reconcile_joined_revenue(["order_id=A1;amount_cents=1500", "order_id=A1;amount_cents=1500", "order_id=A2;amount_cents=2500"])Output
[3, 2, 4000]Example 2
Input
reconcile_joined_revenue(["order_id=B1;amount_cents=1000", "order_id=;amount_cents=9999", "order_id=B2;amount_cents=-250"])Output
[3, 2, 750]