Will this fit in the bay
Goods-in scans a pallet and asks whether the bay it is headed for can take it.
- A missing item, or one with a quantity of zero or less, never fits.
- What is already there plus what is arriving must not exceed capacity.
- Filling the bay exactly is fine.
canStore(item: Item?, onHand: int, capacity: int) → bool
C++ 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.
Where you start
bool canStore(std::optional<Item> item, int onHand, int capacity) {
}
Worked examples
| Call | Result |
|---|---|
canStore(std::optional<Item>(Item{std::string("bolt"), 3}), 0, 100) | true |
canStore(std::optional<Item>(Item{std::string("bolt"), 5}), 10, 15) | true |
canStore(std::optional<Item>(Item{std::string("bolt"), 5}), 10, 12) | false |
canStore(std::optional<Item>(Item{std::string("bolt"), 0}), 0, 100) | false |
Hint
Two guards, then one comparison. Watch the boundary: equal to capacity still fits.
Reference solution in C++
bool canStore(std::optional<Item> item, int onHand, int capacity) {
if (!item.has_value() || item->qty <= 0) return false;
return onHand + item->qty <= capacity;
}