150. Evaluate Reverse Polish Notation
OPERATIONS = ('+', '/', '-', '*')
class Solution:
def calculate(self, x, y, token) -> int:
if token == '+':
return x + y
elif token == '-':
return y - x
elif token == '*':
return x * y
else:
return trunc(y/x)
def evalRPN(self, tokens: List[str]) -> int:
stk = []
for token in tokens:
if token in OPERATIONS:
x, y = stk.pop(), stk.pop()
result = self.calculate(x, y, token)
stk.append(result)
else:
stk.append(int(token))
return stk[-1]