def test_getitem(self): l = range(11) h = MaxHeap() caught_exception = False try: print h[1] except HeapEmptyError: caught_exception = True assert caught_exception == True for i in l: h.add(i) caught_exception = False try: print h[-2] except IndexError: caught_exception = True assert caught_exception == True assert len(h) == 11 caught_exception = False try: print h[11] except IndexError: caught_exception = True assert caught_exception == True x = [10, 9, 5, 6, 8, 1, 4, 0, 3, 2, 7] for i in xrange(11): assert h[i] == x[i]
def test_remove(self): l = [5, 4, 3, 2, 1, 6, 7, 8, 9, 0] h = MaxHeap() caught_exception = False try: assert h.peek() == None except HeapEmptyError as e: assert str(e) == "HeapEmptyError: 'peek(): Heap is empty'" caught_exception = True assert caught_exception == True caught_exception = False try: assert h.remove() == None except HeapEmptyError as e: assert str(e) == "HeapEmptyError: 'remove(): Heap is empty'" caught_exception = True assert caught_exception == True for i in xrange(len(l)): h.add(l[i]) assert len(h) == 10 for i in xrange(9, -1, -1): item = h.remove() assert (len(h) == i) assert item == i
def maxSlidingWindow(nums, k): if not nums: return [] h = MaxHeap() for i in xrange(k): h.add((nums[i], i)) max_list = [h.peek()[0]] for i in xrange(1, len(nums) - k + 1): # max is to the left of window # remove all previous max items that are no longer in the window, # from the heap while h and h.peek()[1] < i: h.remove() # add item at end of current window, i+k-1, to the heap h.add((nums[i + k - 1], i + k - 1)) # add current window's max from the top of the heap max_list.append(h.peek()[0]) return max_list
def test_custom_comparator(self): # Max heap using custom comparator making it a minheap h = MaxHeap(lambda a, b: cmp(b, a)) for i in range(10): h.add(i) assert h.items == range(10) for i in range(10): assert h.remove() == i
def test_add(self): l = [5, 4, 3, 2, 1, 6, 7, 8, 9, 0] intermediate_heaps = [[5], [5, 4], [5, 4, 3], [5, 4, 3, 2], [5, 4, 3, 2, 1], [6, 4, 5, 2, 1, 3], [7, 4, 6, 2, 1, 3, 5], [8, 7, 6, 4, 1, 3, 5, 2], [9, 8, 6, 7, 1, 3, 5, 2, 4], [9, 8, 6, 7, 1, 3, 5, 2, 4, 0]] h = MaxHeap() for i in xrange(len(l)): h.add(l[i]) assert h.items == intermediate_heaps[i]
def test_setitem(self): l = [0, 1, 4, 2, 6, 5, 8, 3, 7, 9, 10] h = MaxHeap() for x in l: h.add(x) ''' 10 / \ 9 6 / \ / \ 4 8 1 5 / \ / \ 0 3 2 7 ''' assert h.items == [10, 9, 6, 4, 8, 1, 5, 0, 3, 2, 7] assert h[4] == 8 h[4] = 11 # calls MaxHeap's decreaseKey ''' 11 / \ 10 6 / \ / \ 4 9 1 5 / \ / \ 0 3 2 7 ''' assert h.items == [11, 10, 6, 4, 9, 1, 5, 0, 3, 2, 7] assert h[0] == 11 h[0] = -1 # calls decreaseKey ''' 10 / \ 9 6 / \ / \ 4 7 1 5 / \ / \ 0 3 2 -1 ''' assert h.items == [10, 9, 6, 4, 7, 1, 5, 0, 3, 2, -1]
def test_increaseKey(self): l = [0, 1, 4, 2, 6, 5, 8, 3, 7, 9, 10] h = MaxHeap() for x in l: h.add(x) ''' 10 / \ 9 6 / \ / \ 4 8 1 5 / \ / \ 0 3 2 7 ''' assert h.items == [10, 9, 6, 4, 8, 1, 5, 0, 3, 2, 7] caught_exception = False try: h.increaseKey(4, 5) except ValueError as v: assert v.message == "ValueError: increaseKey() - New key should be greater than current value" caught_exception = True assert caught_exception == True h.increaseKey(4, 11) ''' 11 / \ 10 6 / \ / \ 4 9 1 5 / \ / \ 0 3 2 7 ''' assert h.items == [11, 10, 6, 4, 9, 1, 5, 0, 3, 2, 7]
def test_decreaseKey(self): l = [0, 1, 4, 2, 6, 5, 8, 3, 7, 9, 10] h = MaxHeap() for x in l: h.add(x) ''' 10 / \ 9 6 / \ / \ 4 8 1 5 / \ / \ 0 3 2 7 ''' assert h.items == [10, 9, 6, 4, 8, 1, 5, 0, 3, 2, 7] caught_exception = False try: h.decreaseKey(4, 10) except ValueError as v: assert v.message == "ValueError: decreaseKey() - New key should be lesser than current value" caught_exception = True assert caught_exception == True h.decreaseKey(0, -1) ''' 9 / \ 8 6 / \ / \ 4 7 1 5 / \ / \ 0 3 2 -1 ''' assert h.items == [9, 8, 6, 4, 7, 1, 5, 0, 3, 2, -1]
def test_custom_items(self): class Record: def __init__(self, a, b, c): self.a = a self.b = b self.c = c # by default, using 'b' as key to compare def __cmp__(self, other): return cmp(self.b, other.b) def __str__(self): return "(%s, %s, %s)" % (self.a, self.b, self.c) h = MaxHeap() h.add(Record("record1", 1, 100)) h.add(Record("record4", 5, 125)) h.add(Record("record3", 2, 50)) h.add(Record("record2", 3, 25)) h.add(Record("record5", 4, 5)) sorted_records_bs = [] h.sorted(lambda x, l: l.append(x.b), sorted_records_bs) assert sorted_records_bs == range(5, 0, -1) h2 = MaxHeap(lambda r1, r2: cmp(r1.a, r2.a)) h2.add(Record("record1", 1, 100)) h2.add(Record("record4", 5, 125)) h2.add(Record("record3", 2, 50)) h2.add(Record("record2", 3, 25)) h2.add(Record("record5", 4, 5)) sorted_records_as = [] h2.sorted(lambda x, l: l.append(x.a), sorted_records_as) assert sorted_records_as == [ "record" + str(i) for i in xrange(5, 0, -1) ] h3 = MaxHeap(lambda r1, r2: cmp(r1.c, r2.c)) h3.add(Record("record1", 1, 100)) h3.add(Record("record4", 5, 125)) h3.add(Record("record3", 2, 50)) h3.add(Record("record2", 3, 25)) h3.add(Record("record5", 4, 5)) sorted_records_cs = [] h3.sorted(lambda x, l: l.append(x.c), sorted_records_cs) assert sorted_records_cs == sorted([100, 125, 50, 25, 5])[::-1]