Smallest box that fits
A shop packs weights (a list of positive whole numbers, in order) into
boxes that each hold at most capacity, filling each box with consecutive
items until the next one would not fit. Write smallest_capacity(weights, boxes)
that returns the smallest capacity for which everything fits into at most
boxes boxes.
smallest_capacity([1, 2, 3, 4, 5], 2) -> 9 (boxes: [1, 2, 3] and [4, 5])
smallest_capacity([1, 2, 3, 4, 5], 5) -> 5 (each item alone; the heaviest sets the floor)
smallest_capacity([7, 2, 5, 10, 8], 2) -> 18
The point is to practise binary search on an answer. A capacity either works or it does not, and if a capacity works so does every larger one. Search the range from the heaviest single item to the total weight.
def smallest_capacity(weights, boxes):
...