Drill

ProblemsPython › inventory

Which parts need attention first

mediuminventoryPython

The morning report lists every part sitting below its minimum, worst first, so the buyer knows where to start.

low_stock(parts: list<Sku>) → list<string>

Solve it in the editor →

Where you start

def low_stock(parts: list[Sku]) -> list[str]:
    

Worked examples

CallResult
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]

The same problem in another language

More inventory problems in Python