-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRPN.py
27 lines (25 loc) · 822 Bytes
/
RPN.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
class evalRPN:
def __init__(self, arr):
self.arr = arr
def evalRPN(self):
operators = ['+','-','*','/']
stack = []
for elem in self.arr:
if (elem not in operators):
stack.append(int(elem))
else:
a = stack.pop()
b = stack.pop()
optr = elem
if (optr == "+"):
stack.append(b + a)
elif (optr == "-"):
stack.append(b - a)
elif (optr == "*"):
stack.append(b * a)
elif (optr == "/"):
stack.append(b / a)
return stack.pop()
if __name__ == "__main__":
object = evalRPN(['2','1','+','3','/'])
print(object.evalRPN())