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.
lowStock(parts: list<Sku>) → list<string>
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.
Where you start
func lowStock(parts []Sku) []string {
}
Worked examples
| Call | Result |
|---|---|
lowStock([]Sku{Sku{Code: "B-2", OnHand: 4, MinLevel: 10}, Sku{Code: "A-1", OnHand: 20, MinLevel: 10}, Sku{Code: "C-3", OnHand: 0, MinLevel: 30}}) | []string{"C-3", "B-2"} |
lowStock([]Sku{Sku{Code: "Z-9", OnHand: 5, MinLevel: 10}, Sku{Code: "A-4", OnHand: 1, MinLevel: 6}}) | []string{"A-4", "Z-9"} |
lowStock([]Sku{Sku{Code: "A-1", OnHand: 10, MinLevel: 10}}) | []string{} |
lowStock([]Sku{}) | []string{} |
Hint
Filter, then sort with a comparator that falls back to the code when the shortfalls match.
Reference solution in Go
func lowStock(parts []Sku) []string {
short := []Sku{}
for _, p := range parts {
if p.OnHand < p.MinLevel {
short = append(short, p)
}
}
sort.Slice(short, func(i, j int) bool {
di, dj := short[i].MinLevel-short[i].OnHand, short[j].MinLevel-short[j].OnHand
if di != dj {
return di > dj
}
return short[i].Code < short[j].Code
})
out := []string{}
for _, p := range short {
out = append(out, p.Code)
}
return out
}