예제 #1
0
        if no1>no:
            median.append(no)
        else:
            h.heap[0],H.heap[0]=H.heap[0],h.heap[0]
            median.append(no1)
        print median

        for line in fd:
	    no=int(line)
            if no<=H.heap[0]:
                H.insert(no)
            elif no>h.heap[0]:
                h.insert(no)
            else:
                H.insert(no)

            if len(H.heap)-len(h.heap)>1:
                reins=H.extractmax()
	        h.insert(reins)
            elif len(h.heap)>len(H.heap):
                reins=h.extractmin()
                H.insert(reins)

            tempmedian=H.heap[0]
#            print "Max and min "+str(tempmedian)
#            print H.heap
#            print h.heap
            print tempmedian
	    median.append(tempmedian)
    print sum(median)%(10000)
예제 #2
0
from heap import MaxHeap
arr=[36, 41, 56, 35, 44, 33, 34, 92, 43, 32, 42]
arr=[1, 9, 3, 10, 4, 20, 2]
arrlength=len(arr)
h=MaxHeap()
count=1 if arrlength>0 else 0
cons=[]
maxcount=count
while len(arr)>0:
    element=arr.pop()
    h.insert(element)
maxi=h.extractmax()
cons.append(maxi)
new=0
for i in range(arrlength-1):
    sec_maxi=h.extractmax()
    if sec_maxi==maxi-1:
        count+=1
        if count>maxcount:
            maxcount=count
        if new==1:
            cons.append(maxi)
            new=0
        cons.append(sec_maxi)
    else:
        count=1
        new=1
        cons=[]
    maxi=sec_maxi
print maxcount
print cons