Python code compiles fine on computer but not in OJ


  • 0
    N

    On my windows machine, both in Python 2.7.5 and Python 3.3.4, my code compiles perfectly fine and solves the sample test cases.

    However, I always get a "Compile Error" in OJ.
    Here is my code:

    class Solution:
    	# @param tokens, a list of string
    	# @return an integer
    	def evalRPN(self, tokens):
    		if len(tokens) == 3:
    			return eval("int(%s %s %s)"%(tokens[0], tokens[2], tokens[1]))
    		if len(tokens) > 3:
    			ind = 0
    			for i,token in enumerate(tokens):
    				if (token == "+" or token == "-" or token == "*" or token == "/" or token == "//"):
    					ind = i; break
    			return self.evalRPN(tokens[0:(ind - 2)] + [str(eval("int(%s %s %s)"%(tokens[ind - 2], tokens[ind], tokens[ind - 1])))] + tokens[ind + 1:])
    

    Any help would be appreciated.
    Thanks.


  • 0

    Do not use eval. You are not supposed to use it to solve this problem.


Log in to reply
 

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