-
Notifications
You must be signed in to change notification settings - Fork 21
/
f.py
78 lines (70 loc) · 1.61 KB
/
f.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import sys
from collections import defaultdict
def sales(sales_data):
customers = defaultdict(lambda: defaultdict(int))
for customer, item, quantity in sales_data:
customers[customer][item] += quantity
return customers
assert sales([
('Ivanov', 'paper', 10),
('Petrov', 'pens', 5),
('Ivanov', 'marker', 3),
('Ivanov', 'paper', 7),
('Petrov', 'envelope', 20),
('Ivanov', 'envelope', 5)
]) == {
'Ivanov': {
'paper': 17,
'marker': 3,
'envelope': 5
},
'Petrov': {
'pens': 5,
'envelope': 20
}
}
assert sales([
("Ivanov", "aaa", 1),
("Petrov", "aaa", 2),
("Sidorov", "aaa", 3),
("Ivanov", "aaa", 6),
("Petrov", "aaa", 7),
("Sidorov", "aaa", 8),
("Ivanov", "bbb", 3),
("Petrov", "bbb", 7),
("Sidorov", "aaa", 345),
("Ivanov", "ccc", 45),
("Petrov", "ddd", 34),
("Ziborov", "eee", 234),
("Ivanov", "aaa", 45)
]) == {
'Ivanov': {
'aaa': 52,
'bbb': 3,
'ccc': 45
},
'Petrov': {
'aaa': 9,
'bbb': 7,
'ddd': 34
},
'Sidorov': {
'aaa': 356
},
'Ziborov': {
'eee': 234
}
}
def main():
sales_data = []
for line in sys.stdin:
sales_item = line.split()
sales_data.append((sales_item[0], sales_item[1], int(sales_item[2])))
results = sales(sales_data)
for customer in sorted(results.keys()):
print(f'{customer}:')
customer_data = results[customer]
for item in sorted(customer_data.keys()):
print(f'{item} {customer_data[item]}')
if __name__ == '__main__':
main()