Kodewick
Start learning →
Mini challenge←→0 / 80

Packing moving boxes

#012Mini challengeFunctionsLoops8 min

When moving house, things go into boxes in order, each box holding at most cap kilos (10 if not given). If the next thing won't fit in the current box, take a new one. Write count_boxes(weights, cap) to return how many boxes you need. Nothing weighs more than cap; nothing to pack means 0.

Examples
count_boxes([4, 5, 3, 6])→2
count_boxes([])→0
main.py
def count_boxes(weights, cap=10):
    boxes = 0
    current = 0
    for w in weights:
        # your code here: if there's no box yet or w won't fit, take a new box (boxes + 1, current back to 0), then put w in
        pass
    return boxes
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.