Fix the promo guard in a cart total
Your task
An AI agent opened a pull request titled "Fix: cart totals sometimes double-count promo discounts". Its description says it reproduced the bug and confirmed the fix. Before you trust that claim, you check the patched function against the rules checkout has to follow.
An assistant wrote apply_promotions(subtotal_cents, promo_codes, promo_percents), the patched function. It is in the editor. It has a bug. Find it and fix it.
Input and output:
subtotal_cents: the cart total before promotions, in cents.promo_codesandpromo_percents: each promotion's code and its percent off, at the same index, in the order they apply.- Return the total in cents after promotions, or -1 when the cart cannot be priced.
Rules the tests check:
- Return -1 if
subtotal_centsis negative or the two lists differ in length. - Skip a promotion whose code is empty or whose percent is below 0 or above 100.
- A code applies at most once. A skipped promotion does not count as applied, so a later valid promotion with the same code still applies. Codes match exactly, case included.
- Apply promotions one after another, each to the running total: the discount is the running total times the percent, divided by 100 and rounded down to whole cents. Subtract it.
- With no promotions applied, return the subtotal.
Examples
apply_promotions(subtotal_cents: int, promo_codes: list[str], promo_percents: list[int]) -> intExample 1
Input
apply_promotions(10000, ["SAVE10", "SAVE10"], [10, 10])Output
9000Example 2
Input
apply_promotions(10000, ["SAVE10", "VIP20"], [10, 20])Output
7200