forked from abeaumont/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddingwords.py
More file actions
executable file
·33 lines (33 loc) · 841 Bytes
/
addingwords.py
File metadata and controls
executable file
·33 lines (33 loc) · 841 Bytes
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
#!/usr/bin/env python2
# https://open.kattis.com/problems/addingwords
a = {}
b = {}
while True:
try:
line = raw_input()
except:
break
fs = line.split()
if fs[0] == 'def':
n = fs[1]
v = int(fs[2])
if n in a: del(b[a[n]])
a[n] = v
b[v] = n
elif fs[0] == 'calc':
r = 0
result = True
if fs[1] not in a: result = False
else: r = a[fs[1]]
for i in range(2, len(fs), 2):
if fs[i] == '=': break
plus = fs[i] == '+'
if fs[i + 1] not in a: result = False
elif plus: r += a[fs[i + 1]]
else: r -= a[fs[i + 1]]
print ' '.join(fs[1:]),
if not result or r not in b: print 'unknown'
else: print b[r]
elif fs[0] == 'clear':
a = {}
b = {}