Example #1
0
class PriorityQueue(object):
    def __init__(self, maxsize=None):
        self.maxsize = maxsize
        self._maxheap = MaxHeap(maxsize)

    def push(self, priority, value):
        entry = (priority, value)
        self._maxheap.add(entry)

    def pop(self, with_priority=False):
        entry = self._maxheap.extract()
        if with_priority:
            return entry
        else:
            return entry[1]

    def is_empty(self):
        return len(self._maxheap) == 0
Example #2
0
def C2W3():
    """Median Maintain"""
    from heap import MinHeap, MaxHeap
    MinHeap = MinHeap()
    MaxHeap = MaxHeap()
    medians = []
    heaplow, heaphigh = [], []
    lowmax, highmin = float('-inf'), float('inf')
    with open('Median.txt') as file:
        for line in file:
            item = int(line)
            lenlow, lenhigh = len(heaplow), len(heaphigh)
            while True:
                if lenlow > lenhigh:
                    if item >= lowmax:
                        MinHeap.add(heaphigh, item)
                        highmin = heaphigh[0]
                        break
                    if item < lowmax:
                        returnitem = MaxHeap.popadd(heaplow, item)
                        MinHeap.add(heaphigh, returnitem)
                        lowmax = heaplow[0]
                        highmin = heaphigh[0]
                        break
                if lenlow <= lenhigh:
                    if item <= highmin:
                        MaxHeap.add(heaplow, item)
                        lowmax = heaplow[0]
                        break
                    if item > highmin:
                        returnitem = MinHeap.popadd(heaphigh, item)
                        MaxHeap.add(heaplow, returnitem)
                        lowmax = heaplow[0]
                        highmin = heaphigh[0]
                        break
            medians.append(lowmax)
            print('item', item, 'lowmax', lowmax, 'highmin', highmin)
    return sum(medians), len(medians)