2

So i wrote this code and it passes the first test case, and fails all the rest. However, I can't seem to find an input that breaks it. Maybe it's because I've been staring at the code too long, but i would appreciate any help. The algorithm uses two priority queues for the smallest and largest halves of the current list. Here's the code:

#!/bin/python
import heapq

def fix(minset, maxset):
    if len(maxset) > len(minset):
        item = heapq.heappop(maxset)
        heapq.heappush(minset, -item)
    elif len(minset) > (len(maxset) + 1):
        item = heapq.heappop(minset)
        heapq.heappush(maxset, -item)

N = int(raw_input())

s = []
x = []

for i in range(0, N):

        tmp = raw_input()
        a, b = [xx for xx in tmp.split(' ')]
        s.append(a)
        x.append(int(b))

minset = []
maxset = []

for i in range(0, N):
    wrong = False
    if s[i] == "a":
        if len(minset) == 0:
            heapq.heappush(minset,-x[i])
        else:
            if x[i] > minset[0]:
                heapq.heappush(maxset, x[i])
            else:
                heapq.heappush(minset, -x[i])
        fix(minset, maxset)
    elif s[i] == "r":
        if -x[i] in minset:
            minset.remove(-x[i])
            heapq.heapify(minset)
        elif x[i] in maxset:
            maxset.remove(x[i])
            heapq.heapify(maxset)
        else:
            wrong = True
        fix(minset, maxset)
    if len(minset) == 0 and len(maxset) == 0:
        wrong = True

    if wrong == False:
        #Calculate median
        if len(minset) > len(maxset):
            item = - minset[0]
            print int(item)
        else:
            item = ((-float(minset[0])) + float(maxset[0])) / 2
            if item.is_integer():
                print int(item)
                continue
            out =  str(item)
            out.rstrip('0')
            print out
    else:
        print "Wrong!"

1 Answers1

0

Your original was not so legible, so first I made it object-oriented: MedianHeapq supports methods rebalance(), add(), remove(), size(), median(). We seriously want to hide the members minset,maxset from the client code, for all sorts of sensible reasons: prevent client from swapping them, modifying them etc. If client needs to see them you just write an accessor. We also added a __str__() method which we will use to debug visually and make your life easier.

Also added legibility changes to avoid the indexing with [i] everywhere, rename s,x arrays to op,val, add prompts on the raw_input(), reject invalid ops at the input stage. Your actual computation of the median confuses me (when do you want float and when integer? the rstrip('0') is a bit wack), so I rewrote it, change that if you want something else. A discussion of the algorithm is here.

Now it is legible and self-contained. Also makes it testable. You might be making sign errors in your code, I don't know, I'll look at that later. Next we will want to automate it by writing some PyUnit testcases. doctest is also a possibility. TBC.

Ok I think I see a bug in the sloppiness about locating the median. Remember the minset and maxset can have a size mismatch of +/-1. So take more care about precisely where the median is located.

#!/bin/python
import heapq    

class MedianHeapq(object):
    def __init__(self):
        self.minset = []
        self.maxset = []        
    def rebalance(self):
        size_imbalance = len(self.maxset) - len(self.minset)
        if len(self.maxset) > len(self.minset):
        #if size_imbalance > 0:
            item = heapq.heappop(self.maxset)
            heapq.heappush(self.minset, -item)
        #elif size_imbalance < -1:
        elif len(self.minset) > (len(self.maxset) + 1):
            item = heapq.heappop(self.minset)
            heapq.heappush(self.maxset, -item)
    def add(self, value, verbose=False):
        if len(self.minset) == 0:
            heapq.heappush(self.minset,-value)
        else:
            if value > self.minset[0]:
                heapq.heappush(self.maxset, value)
            else:
                heapq.heappush(self.minset, -value)
        self.rebalance()
        if verbose: print self.__str__()
        return False
    def remove(self,value,verbose=False):
        wrong = False
        if -value in self.minset:
            minset.remove(-value)
            heapq.heapify(self.minset)
        elif value in maxset:
            maxset.remove(value)
            heapq.heapify(self.maxset)
        else:
            wrong = True
        self.rebalance()
        if verbose: print self.__str__()
        return wrong
    def size(self):
        return len(self.minset)+len(self.maxset)
    def median(self):
        if len(self.minset) > len(self.maxset):
            item = - self.minset[0]
            return int(item)
        else:
            item = (-self.minset[0] + self.maxset[0]) / 2.0
            # Can't understand the intent of your code here: int, string or float?
            if item.is_integer():
                return int(item)
            #    continue # intent???
            else:
               return item
            # The intent of this vv seems to be round floats and return '%.1f' % item ?? 
            #out =  str(item)
            #out.rstrip('0') # why can't you just int()? or // operator?
            #return out


    def __str__(self):
        return 'Median: %s Minset:%s Maxset:%s' % (self.median(), self.minset,self.maxset)

# Read size and elements from stdin
N = int(raw_input('Size of heap? '))
op = []
val = []
while(len(val)<N):
        tmp = raw_input('a/r value : ')
        op_, val_ = tmp.split(' ')
        if op_ not in ['a','r']: # reject invalid ops
            print 'First argument (operation) must be a:Add or r:Remove! '
            continue
        op.append(op_)
        val.append(int(val_))

mhq = MedianHeapq()
for op_,val_ in zip(op,val): # use zip to avoid indexing with [i] everywhere
    wrong = False
    if op_ == 'a':
        wrong = mhq.add(val_)
    elif op_ == 'r':
        wrong = mhq.remove(val_)

    assert (mhq.size()>0), 'Heap has zero size!'

    assert (not wrong), 'Heap structure is wrong!'

    if not wrong:
        print mhq.__str__()        
Community
  • 1
  • 1
smci
  • 26,085
  • 16
  • 96
  • 138