DAY 01 OF 100 · Integer arithmetic · money

Easy

The Receipt That Must Balance.

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.

  1. Each line contains price_cents and quantity. Calculate its cost using integers, then add all line costs.
  2. Apply the fixed coupon once to the whole basket. A coupon cannot reduce the amount due below zero.
  3. 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}
day-1.py
Python 3
Tab inserts four spaces. Press Escape, then Tab to move to the next control, or Shift+Tab to move to the previous control.

Log in or create an account to run code, submit solutions, and save your progress.

Output

Run your code to see the result here.