Drill

ProblemsPython › inventory

What the shelf is worth

easyinventoryPython

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

stock_value(rows: list<Row>) → int

Solve it in the editor →

Where you start

def stock_value(rows: list[Row]) -> int:
    

Worked examples

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

The same problem in another language

More inventory problems in Python