forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariablearithmetic.py
38 lines (33 loc) · 961 Bytes
/
variablearithmetic.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
import fileinput
def main():
words = {}
for line in fileinput.input():
if line == '0\n':
break
if '=' in line:
word, eq, val = line.split()
words[word] = int(val)
else:
line = line.split()
line = line[0::2]
extrawords = []
total = 0
for i in line:
try:
total += int(i)
except:
if i in words:
total += words[i]
else:
extrawords.append(i)
if total != 0:
print(total, end=' ')
for i in extrawords:
print('+', i, end=' ')
else:
print(extrawords[0], end=' ')
for i in extrawords[1:]:
print('+', i, end=' ')
print()
if __name__ == "__main__":
main()