Predict the output
What does this print?
weights = [3, 2, 2, 4, 1, 4]
def days_for(cap: int) -> int:
days, load = 1, 0
for w in weights:
if load + w > cap:
days, load = days + 1, 0
load += w
return days
print(days_for(6), days_for(7), days_for(16))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 3 3 1
Right: 6 gives [3, 2] [2, 4] [1, 4], 7 gives [3, 2, 2] [4, 1] [4], 16 takes everything at once.
Day counts never rise as capacity grows, which is the monotonic property the binary search depends on.
Why not the others?
- B 4 3 1. At capacity 6 the parcels fit in three runs.
- C 3 2 1. At capacity 7 the last parcel needs a third day.