Drill

ProblemsGo › inventory

Will this fit in the bay

easyinventoryMathGo

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

canStore(item: Item?, onHand: int, capacity: int) → bool

Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

func canStore(item *Item, onHand int, capacity int) bool {
	
}

Worked examples

CallResult
canStore(&Item{Name: "bolt", Qty: 3}, 0, 100)true
canStore(&Item{Name: "bolt", Qty: 5}, 10, 15)true
canStore(&Item{Name: "bolt", Qty: 5}, 10, 12)false
canStore(&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 Go
func canStore(item *Item, onHand int, capacity int) bool {
	if item == nil || item.Qty <= 0 {
		return false
	}
	return onHand+item.Qty <= capacity
}

The same problem in another language

More inventory problems in Go