Drill

ProblemsPython › inventory

Will this fit in the bay

easyinventoryPython

Goods-in scans a pallet and asks whether the bay it is headed for can take it.

can_store(item: Item?, on_hand: int, capacity: int) → bool

Solve it in the editor →

Where you start

def can_store(item: Item | None, on_hand: int, capacity: int) -> bool:
    

Worked examples

CallResult
can_store(Item(name="bolt", qty=3), 0, 100)True
can_store(Item(name="bolt", qty=5), 10, 15)True
can_store(Item(name="bolt", qty=5), 10, 12)False
can_store(Item(name="bolt", qty=0), 0, 100)False

Hint

Two guards, then one comparison. Watch the boundary: equal to capacity still fits.

Reference solution in Python
def can_store(item: Item | None, on_hand: int, capacity: int) -> bool:
    if item is None or item.qty <= 0:
        return False
    return on_hand + item.qty <= capacity

The same problem in another language

More inventory problems in Python