-
Notifications
You must be signed in to change notification settings - Fork 0
/
postfix.py
40 lines (30 loc) · 997 Bytes
/
postfix.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
def moveBack(i, expression):
for index in range(i-1, len(expression)-2):
expression[index] = expression[index+2]
expression[-2] = 0
expression[-1] = 0
return expression
def nZeros(expression):
zeros = 0
for i in range(len(expression)):
if expression[i] == 0:
zeros += 1
return zeros
n = int(input())
expression = input().split()
i = 0
while nZeros(expression) != n - 1:
if expression[i] == "+":
expression[i-2] = (int(expression[i-2])+int(expression[i-1]))
expression = moveBack(i, expression)
i-= 2
elif expression[i] == "-":
expression[i-2] = (int(expression[i-2])-int(expression[i-1]))
expression = moveBack(i, expression)
i-= 2
elif expression[i] == "*":
expression[i-2] = (int(expression[i-2])*int(expression[i-1]))
expression = moveBack(i, expression)
i-= 2
i += 1
print(expression[0])