My Python solution can not pass a testcase, please help


  • 0
    C
    class Solution(object):
        def evalRPN(self, tokens):
            """
            :type tokens: List[str]
            :rtype: int
            """
            stack = []
            for i in tokens:
                if i not in ['+','-', '*', '/']:
                    stack.append(i)
                else:
                    b = int(stack.pop())
                    a = int(stack.pop())
                    if i == '+':
                        c = a+ b
                    elif i == '-':
                        c = a- b
                    elif i == '*':
                        c = a* b
                    elif i == '/':
                        c = a /b
                    stack.append(int(c))
            return int(stack[0])
    

    this is my Python solution, I can not pass the testcase as below:

    Input: ["10","6","9","3","+","-11","","/","","17","+","5","+"]
    Output: 12
    Expected: 22

    But I run my code on my computer with Python3, It works ,for this testcase, the output is 22. I can not understand, why my code can not pass the testcase on leetcode? thanks


  • 0
    K

    @chrisbarry
    using python2 check the output for -2 // 5 and 2 // 5


  • 1

    try c = int(float(a)/b)
    this code would make the output of division the same as JAVA


  • 0
    C

    @han36 thanks it works


Log in to reply
 

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