Drill

ProblemsC# › inventory

Which parts need attention first

mediuminventoryArraysSortingC#

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

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

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.

Solve it in Python →

Where you start

public List<string> LowStock(List<Sku> parts) {
    
}

Worked examples

CallResult
LowStock(new List<Sku> { new Sku("B-2", 4, 10), new Sku("A-1", 20, 10), new Sku("C-3", 0, 30) })new List<string> { "C-3", "B-2" }
LowStock(new List<Sku> { new Sku("Z-9", 5, 10), new Sku("A-4", 1, 6) })new List<string> { "A-4", "Z-9" }
LowStock(new List<Sku> { new Sku("A-1", 10, 10) })new List<string> { }
LowStock(new List<Sku> { })new List<string> { }

Hint

Filter, then sort with a comparator that falls back to the code when the shortfalls match.

Reference solution in C#
public List<string> LowStock(List<Sku> parts) {
    var shortList = parts.Where(p => p.OnHand < p.MinLevel).ToList();
    shortList.Sort((a, b) => {
        int d = (b.MinLevel - b.OnHand) - (a.MinLevel - a.OnHand);
        return d != 0 ? d : string.CompareOrdinal(a.Code, b.Code);
    });
    return shortList.Select(p => p.Code).ToList();
}

The same problem in another language

More inventory problems in C#