-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducer3.py
45 lines (32 loc) · 891 Bytes
/
reducer3.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
#!/usr/bin/env python3
import sys
# previtem=None
# total=0
# for line in sys.stdin:
# item,quantity=line.strip().split()
# quantity=int(quantity)
# if previtem==item:
# total=total+quantity
# else:
# if (previtem):
# print(previtem,total)
# previtem=item
# total=quantity
# if (previtem):
# print(previtem,total,sep="\t")
#!/usr/bin/env python3
import sys
prev_product = None
balance = 0
for line in sys.stdin:
product, amount = line.strip().split()
product = product.split('+')[0]
amount = int(amount)
if prev_product != product:
if prev_product is not None:
print(prev_product,balance,sep="\t")
prev_product = product
balance = 0
balance += amount
if prev_product is not None:
print(prev_product,balance,sep="\t")