-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShopping_cart.py
47 lines (38 loc) · 1.2 KB
/
Shopping_cart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
class Item:
def __init__(self, name: str, price: int):
self.name = name
self.price = price
class ShoppingCart:
def __init__(self):
self.items = []
def add(self, item: Item):
self.items.append(item)
def total(self) -> int:
return sum(item.price for item in self.items)
def __len__(self):
return len(self.items)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
items = []
for _ in range(n):
name, price = input().split()
item = Item(name, int(price))
items.append(item)
cart = ShoppingCart()
q = int(input())
for _ in range(q):
line = input().split()
command, params = line[0], line[1:]
if command == "len":
fptr.write(str(len(cart)) + "\n")
elif command == "total":
fptr.write(str(cart.total()) + "\n")
elif command == "add":
name = params[0]
item = next(item for item in items if item.name == name)
cart.add(item)
else:
raise ValueError("Unknown command %s" % command)
fptr.close()