LC 0150 [M] Evaluate Reverse Polish Notation - ALawliet/algorithms GitHub Wiki

class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        stack = []
        for x in tokens:
            if x not in '+-*/': # isnumeric() doesn't work for neg numbers (-x)
                stack.append(int(x))
            else:
                op = x
                b = stack.pop()
                a = stack.pop()
                if op == '+':
                    stack.append(a+b)
                elif op == '-':
                    stack.append(a-b)
                elif op == '*':
                    stack.append(a*b)
                elif op == '/':
                    stack.append(int(a/b))
        return stack.pop()