Fix the checkout price function after a discount change

Review the AI's codeEasy10 min

Your task

You asked an AI to add a discount to your checkout's price function. It returned 30 lines where yours had 12, reformatted from top to bottom. A diff that size can change more than you asked for, so you check the new version against everything the function must still do.

An assistant wrote calculate_price_cents(unit_price_cents, quantities, discount_percent). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • unit_price_cents: the unit price of each line item, in cents.
  • quantities: the quantity of each line item. Item i uses index i of both lists.
  • discount_percent: the discount, as a whole number percent.
  • Return the total in cents, as an integer.

Rules the tests check:

  • When the lists differ in length, only the items present in both count. Extra entries are ignored.
  • Skip an item whose price or quantity is negative.
  • The subtotal is the sum of price times quantity over the items that count.
  • Clamp the discount percent to 0 to 100: below 0 counts as 0, above 100 counts as 100.
  • The discount amount is the subtotal times the percent, divided by 100 and rounded down. The net is the subtotal minus the discount amount.
  • The tax is the net times 85, divided by 1000 and rounded down (8.5 percent). Return the net plus the tax.

Examples

calculate_price_cents(unit_price_cents: list[int], quantities: list[int], discount_percent: int) -> int
Example 1
Inputcalculate_price_cents([1000, 250], [2, 4], 0)
Output3255
Example 2
Inputcalculate_price_cents([1000, 250], [2, 4], 10)
Output2929
Example 3
Inputcalculate_price_cents([], [], 20)
Output0