Python - Clean code using stack and lambdas


  • 0
    I
    class Solution(object):
        def evalRPN(self, tokens):
            """
            :type tokens: List[str]
            :rtype: int
            """
            operations = {
                "*": lambda x,y: x*y,
                "+": lambda x,y: x+y,
                "-": lambda x,y: x-y,
                "/": lambda x,y: float(x)/y
            }
    
            stack = []
            for token in tokens:
                if token not in operations:
                    stack.append(int(token))
                else:
                    right = stack.pop()
                    left = stack.pop()
                    result = operations[token](left, right)
                    stack.append(int(result))
            return stack.pop()
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.