← Back to challenges

How Many Boxes?

PythonHardarraysmath

Instructions

You work in a factory, and your job is to take items from a conveyor belt and pack them into boxes. Each box can hold a maximum of 10 kgs. Given a list containing the weight (in kg) of each item, how many boxes would you need to pack all of the items?

Example

boxes([2, 1, 2, 5, 4, 3, 6, 1, 1, 9, 3, 2]) ➞ 5

# Box 1 = [2, 1, 2, 5] (10kg)
# Box 2 = [4, 3] (7kg)
# Box 3 = [6, 1, 1] (8kg)
# Box 4 = [9] (9kg)
# Box 5 = [3, 2] (5kg)

Notes

There will always be a minimum of 1 item to pack. All items will weigh less than or equal to 10 kgs, and need to be packed in the order that they arrive.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.