Codington
0 of 6 problems solved • 0%

Exercise prompt

Apply Stock Changes

Each delivery or sale changes a product's balance. A negative change records a sale. Starting with a copy of the stock dictionary preserves the original inventory.

Example

An edited copy lets you compare the before and after counts.

opening = {"red": 3} closing = opening.copy() closing["red"] -= 1 print(opening["red"], closing["red"]) # 3 2 print(closing.get("blue", 0)) # 0

A missing key can have a starting value of zero without changing the original dictionary.

Your task

Implement apply_changes(stock, changes). Each change is an (item, delta) pair. Return the new counts after applying every change in order. A new item starts at zero. Keep entries that reach zero and leave the input dictionary untouched.

apply_changes({"tea": 4}, [("tea", -4), ("milk", 3)]) # {"tea": 0, "milk": 3}