Which parts need attention first
The morning report lists every part sitting below its minimum, worst first, so the buyer knows where to start.
- A part is short when what is on hand is strictly below its minimum.
- Order by shortfall, largest first.
- Parts equally short are listed by code, A to Z.
low_stock(parts: list<Sku>) → list<string>
Where you start
def low_stock(parts: list[Sku]) -> list[str]:
Worked examples
| Call | Result |
|---|---|
low_stock([Sku(code="B-2", on_hand=4, min_level=10), Sku(code="A-1", on_hand=20, min_level=10), Sku(code="C-3", on_hand=0, min_level=30)]) | ["C-3", "B-2"] |
low_stock([Sku(code="Z-9", on_hand=5, min_level=10), Sku(code="A-4", on_hand=1, min_level=6)]) | ["A-4", "Z-9"] |
low_stock([Sku(code="A-1", on_hand=10, min_level=10)]) | [] |
low_stock([]) | [] |
Hint
Filter, then sort with a comparator that falls back to the code when the shortfalls match.
Reference solution in Python
def low_stock(parts: list[Sku]) -> list[str]:
short = [p for p in parts if p.on_hand < p.min_level]
short.sort(key=lambda p: (-(p.min_level - p.on_hand), p.code))
return [p.code for p in short]