DAY 02 OF 100 · Conditions · records

Easy

Restock Before the Shelf Is Empty.

Today’s challenge

Build the solution

Write a function called restock(items) that decides which products need an order.

Return contract: A list of dictionaries containing sku and order_quantity, omitting products that need no order. The supplied driver wraps this list as orders and adds input_unchanged to verify that the original items are preserved.

Input bounds: 0–200 items; unique string SKUs; 0 ≤ quantity, minimum ≤ target ≤ 10,000.

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. An item needs replenishment only when quantity is strictly below minimum.
  2. For a qualifying item, order target minus quantity units. Targets are always at least the minimum.
  3. Keep qualifying items in their original order. Do not change the input items.

The Twist: A shelf exactly at its minimum is acceptable today. Ordering it early changes the shop’s purchasing decision.

Sample

Input

{"items":[{"minimum":5,"quantity":3,"sku":"tea","target":12},{"minimum":5,"quantity":5,"sku":"rice","target":20}]}

Expected output

{"input_unchanged":true,"orders":[{"order_quantity":9,"sku":"tea"}]}
day-2.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.