Inconsistent output between the online interpreter and my Mac for the same input


  • 0
    Q

    class MedianFinder:
    tupNum = []

    def __init__(self):
      '''Do nothing'''
    
    def addNum(self, num):
      intLen = len(self.tupNum)
      
      if intLen == 0:
        self.tupNum.append(num)
      else:
        intSrt = 0
        intEnd = intLen - 1
    
        while (intSrt <= intEnd):
          intMid = (intSrt + intEnd) / 2
          if (self.tupNum[intMid] < num):
            intSrt = intMid + 1
          else:
            intEnd = intMid - 1
    
        self.tupNum.insert(intSrt, num)
    
    def findMedian(self):
      intLen = len(self.tupNum)
    
      if intLen % 2 == 0:
        return ((float(self.tupNum[intLen/2 - 1]) + float(self.tupNum[intLen/2]))/2)
      else:
        return (float(self.tupNum[(intLen - 1)/2]))
    

    Above is my code in Python. On my Mac, it returns 2 when only 2 is added and returns 2.5 when both 2 and 3 are there, which is as expected. However, when the same code runs on the online interpreter, it returns 0.5 and 1 for the same input, but I cannot figure out why. Can anyone help? Thanks!


  • 0

    See the FAQ, particularly the part about static variables.


Log in to reply
 

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