Today’s challenge
Build the solution
Write a function called checkout(lines, coupon_cents) for a small shop checkout.
Return contract: A dictionary with integer keys subtotal_cents, discount_cents, and total_cents.
Input bounds: 0–200 lines; prices and coupon are 0–1,000,000 cents; quantities are 0–1,000. Inputs have the documented fields.
Keep the supplied driver below your implementation. It reads one JSON object and prints your return value as JSON. The driver may also probe the behaviour described above; those probe fields are part of the test input.
- Each line contains price_cents and quantity. Calculate its cost using integers, then add all line costs.
- Apply the fixed coupon once to the whole basket. A coupon cannot reduce the amount due below zero.
- Return the subtotal, the discount actually used, and the final amount due. An empty basket has three zero amounts.
The Twist: The coupon can be larger than the basket. The receipt must still balance: subtotal = discount + total.
Sample
Input
{"coupon_cents":100,"lines":[{"price_cents":199,"quantity":3},{"price_cents":250,"quantity":2}]}Expected output
{"discount_cents":100,"subtotal_cents":1097,"total_cents":997}