Problem 143113 · medium · Level 01 Prerequisites & Setup

Into the First Van With Room

for-else · break · lists of lists · py-loops · py-lists

At a depot, parcels come off the belt one at a time and are loaded straight into vans. Every van can carry at most capacity kilograms. The loaders follow one rule: put the parcel into the first van, in the order the vans were opened, that still has enough room for it; if no open van has room, open a new van for it. Vans are never closed.

Write load_vans(parcels, capacity) that returns the loads of all vans, in the order they were opened, each load being the list of parcel weights in the order they went in.

The helper parcel_run(n, capacity, seed) returns n random parcel weights.

Examples

Input:  parcels = [4, 8, 1, 4, 2, 1], capacity = 10
Output: [[4, 1, 4, 1], [8, 2]]
Explanation: 8 does not fit next to 4 (6 kg of room), so van 2 opens. The 2 does
not fit into van 1 (1 kg left) but fits exactly into van 2.

Input:  parcels = [], capacity = 5
Output: []

Constraints

  • 0 <= len(parcels) <= 1000
  • 1 <= capacity <= 1000 and every parcel weighs between 1 and capacity kilograms

Goals

  • Use for ... else for 'searched every van and none had room'
  • Keep a list of lists and append to the right inner list
Starting Python…