DAY 06 OF 100 · Nested loops · FIFO queues

Medium

Clear the Backorder Queue.

Today’s challenge

Build the solution

Write a function called pack_orders(orders, capacities).

Return contract: A dictionary with days (records with day, shipments, unused_capacity) and remaining (records with id, quantity). Day numbers start at 1; shipment records contain id and quantity. The supplied driver adds input_unchanged to check that neither input list was changed.

Input bounds: 0–100 unique-ID orders; quantities 1–1,000; 0–100 capacities, each 0–1,000. Do not mutate either input.

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 order has id and a positive quantity. Process orders in their original order.
  2. Each capacity is the number of units that can be packed on that day. An order may be split across days, but a later order cannot receive units before earlier orders are filled.
  3. Produce a record for every supplied day, including days with zero capacity or no shipments. Return any unfinished order quantities afterward.

The Twist: The day can end halfway through an order. Tomorrow must resume that exact remaining quantity rather than start the order again.

Sample

Input

{"capacities":[3,4,5],"orders":[{"id":"a","quantity":5},{"id":"b","quantity":4}]}

Expected output

{"days":[{"day":1,"shipments":[{"id":"a","quantity":3}],"unused_capacity":0},{"day":2,"shipments":[{"id":"a","quantity":2},{"id":"b","quantity":2}],"unused_capacity":0},{"day":3,"shipments":[{"id":"b","quantity":2}],"unused_capacity":3}],"input_unchanged":true,"remaining":[]}
day-6.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.