Inventory commands

Run a shop inventory. add ITEM K adds stock. remove ITEM K prints OK and removes stock, or prints NOT ENOUGH and changes nothing. count ITEM prints the current quantity (0 for unknown items).

What to build

An inventory driven by commands.

Requirements

  • add ITEM K adds stock.
  • remove ITEM K prints OK, or NOT ENOUGH and changes nothing.
  • count ITEM prints the quantity, or 0 for an unknown item.

Input

The first line has n (1 to 1000). Each of the next n lines is a command.

Output

One line for each remove and count command.

Constraints

n is from 1 to 1000.

Example

Input:

4
add pen 10
remove pen 3
remove pen 9
count pen

Output:

OK
NOT ENOUGH
7

Why it works

get(item, 0) treats a new item as having no stock, so all three commands stay short.

Examples

Example 1 · Input
4
add pen 10
remove pen 3
remove pen 9
count pen
Expected output
OK
NOT ENOUGH
7
Example 2 · Input
1
count book
Expected output
0

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Methods add, remove and count, each a few lines.

Hint 2

self.stock.get(item, 0) handles unknown items.

Solution

Show one correct solution (try it yourself first)
class Inventory:
    def __init__(self):
        self.stock = {}

    def add(self, item, amount):
        self.stock[item] = self.stock.get(item, 0) + amount

    def remove(self, item, amount):
        if self.stock.get(item, 0) < amount:
            return False
        self.stock[item] -= amount
        return True

    def count(self, item):
        return self.stock.get(item, 0)

shop = Inventory()
n = int(input())
for _ in range(n):
    parts = input().split()
    if parts[0] == "add":
        shop.add(parts[1], int(parts[2]))
    elif parts[0] == "remove":
        print("OK" if shop.remove(parts[1], int(parts[2])) else "NOT ENOUGH")
    else:
        print(shop.count(parts[1]))

Lesson: Project: a shop inventory →