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.
stock_value(rows: list<Row>) → int
Where you start
def stock_value(rows: list[Row]) -> int:
Worked examples
| Call | Result |
|---|---|
stock_value([Row(qty=3, unit_cost=250), Row(qty=2, unit_cost=100)]) | 950 |
stock_value([Row(qty=-4, unit_cost=500), Row(qty=1, unit_cost=500)]) | 500 |
stock_value([Row(qty=0, unit_cost=900)]) | 0 |
stock_value([]) | 0 |
Hint
One pass, one accumulator, one guard.
Reference solution in Python
def stock_value(rows: list[Row]) -> int:
return sum(r.qty * r.unit_cost for r in rows if r.qty > 0)