Why this code is Compile Error with python?


  • 0
    A
    class Solution:
    # @param tokens, a list of string
    # @return an integer
    def evalRPN(self, tokens):
        tab = "+-*/"
        stack = []
        for token in tokens:
            if tab.find(token) == -1:
                stack.append(int(token))
            else:
                tmp = stack.pop()
                stack.append(eval('%d%s%d'%(stack.pop(), token, tmp)))
        return stack[0]

  • 0
    G

    use of eval is forbidden


  • 0
    L

    thank you. It really bothered me a lot.


Log in to reply
 

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