-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathreverse-polish-notation.py
60 lines (45 loc) · 1.56 KB
/
reverse-polish-notation.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
"""
#163
Jane Street
Given an arithmetic expression in Reverse Polish Notation, write a program to evaluate it.
The expression is given as a list of numbers and operands. For example: [5, 3, '+'] should return 5 + 3 = 8.
For example, [15, 7, 1, 1, '+', '-', '/', 3, '*', 2, 1, 1, '+', '+', '-'] should return 5, since it is equivalent to ((15 / (7 - (1 + 1))) * 3) - (2 + (1 + 1)) = 5.
You can assume the given expression is always valid.
"""
class Stack:
def __init__(self):
self.array = []
def push(self, val):
self.array.append(val)
def pop(self):
if len(self.array):
return self.array.pop()
else:
return None
def calculate(operand1, operand2, operator):
if operator == '+':
return operand1 + operand2
elif operator == '-':
return operand1 - operand2
elif operator == '*':
return operand1 * operand2
elif operator == '/':
return operand1 / operand2
def evaluate(exprList):
operators = ['+', '-', '*', '/']
evalStack = Stack()
for token in exprList:
if token in operators:
operand2 = evalStack.pop()
operand1 = evalStack.pop()
evalStack.push(calculate(operand1, operand2, token))
else:
evalStack.push(token)
return evalStack.pop()
def main():
expressionList = [5, 3, '+']
print(evaluate(expressionList))
expressionList = [15, 7, 1, 1, '+', '-', '/', 3, '*', 2, 1, 1, '+', '+', '-']
print(evaluate(expressionList))
if __name__ == "__main__":
main()