What the shelf is worth
Month-end valuation multiplies what is on each shelf by its unit cost and adds it up.
- Quantity and cost are both in whole units; the answer is in minor currency units.
- Negative quantities are counting errors — skip those rows entirely.
- An empty shelf is worth nothing.
stockValue(rows: list<Row>) → int
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 stockValue(rows []Row) int {
}
Worked examples
| Call | Result |
|---|---|
stockValue([]Row{Row{Qty: 3, UnitCost: 250}, Row{Qty: 2, UnitCost: 100}}) | 950 |
stockValue([]Row{Row{Qty: -4, UnitCost: 500}, Row{Qty: 1, UnitCost: 500}}) | 500 |
stockValue([]Row{Row{Qty: 0, UnitCost: 900}}) | 0 |
stockValue([]Row{}) | 0 |
Hint
One pass, one accumulator, one guard.
Reference solution in Go
func stockValue(rows []Row) int {
total := 0
for _, r := range rows {
if r.Qty > 0 {
total += r.Qty * r.UnitCost
}
}
return total
}