← Gym/Inventory Processor
00:00/ 15 min

🧭 Do not search for the first 15 minutes. When stuck: re-read the requirements → define I/O → choose the data structure → trace a small example by hand → write code.

Implement build_inventory(operations), which applies stock movements in order to produce the final inventory. Reading the rules precisely is the whole problem.

Input

python
operations = [
    ("apple", "add", 10),
    ("apple", "remove", 3),
    ("banana", "add", 5),
    ("apple", "add", 2),
]

Each entry is (item, "add"|"remove", quantity) and the quantity is a positive integer.

Rules

  • add increases stock; remove decreases it.
  • A remove that would drive stock negative is ignored entirely. You do not deduct what is available — you skip the whole operation.
  • After applying every entry, drop items whose final stock is 0.
  • Surviving items keep their order of first appearance.

Output

python
{"apple": 9, "banana": 5}

Level 1 · Applying stock movements

Implement build_inventory(operations).

  • No entries returns an empty dictionary.
  • A remove on an item that was never stocked is ignored.