Drill

ProblemsC# › inventory

What the shelf is worth

easyinventoryArraysMathC#

Month-end valuation multiplies what is on each shelf by its unit cost and adds it up.

StockValue(rows: list<Row>) → int

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 int StockValue(List<Row> rows) {
    
}

Worked examples

CallResult
StockValue(new List<Row> { new Row(3, 250), new Row(2, 100) })950
StockValue(new List<Row> { new Row(-4, 500), new Row(1, 500) })500
StockValue(new List<Row> { new Row(0, 900) })0
StockValue(new List<Row> { })0

Hint

One pass, one accumulator, one guard.

Reference solution in C#
public int StockValue(List<Row> rows) {
    int total = 0;
    foreach (var r in rows) if (r.Qty > 0) total += r.Qty * r.UnitCost;
    return total;
}

The same problem in another language

More inventory problems in C#