Bug in OJ! Python code gives different answer from my local PC


  • 0
    R

    I run this code and got correct answer for "2-(5-6)"=3 on my PC. However, OJ runs my code and gives answer -9, which is absurd...
    Why the "print" function does not work now in OJ?

    class Solution(object):
        def calculate(self, s):
            """
            :type s: str
            :rtype: int
            """
    
            # Build poland reverse
    
            def cal_until_right_par(s,i):
                num = 0
                res = 0
                op = '+'
                op_d = {'+':lambda x,y: x+y,'-':lambda x,y: x-y}
    
                while i < len(s):
                    if s[i] in '+-':
                        res = op_d[op](res,num)
                        num = 0
                        op = s[i]
                    if s[i] in '0123456789':
                        num = num *10 + int(s[i])
                    if s[i] is ')':
                        res = op_d[op](res,num)
                        num = 0
                        return res,i
                    if s[i] is '(':
                        print op
                        num,i = cal_until_right_par(s,i+1)
                        print op
                    i += 1
                res = op_d[op](res,num)
                return res,i
    
            return cal_until_right_par(s,0)[0]
    
    sol = Solution()
    print sol.calculate('2-(5-6)')

  • 1

    Your bug is that you use is instead of == for string comparison.


  • 0
    R

    Thank you for help! My bad...


Log in to reply
 

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