Drill

ProblemsGo › inventory

What the shelf is worth

easyinventoryArraysMathGo

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

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.

Solve it in Python →

Where you start

func stockValue(rows []Row) int {
	
}

Worked examples

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

The same problem in another language

More inventory problems in Go