def test_pop_heap(): heap1 = Heap(num_list) heap2 = Heap() heap3 = Heap() heap2.extend(num_list) for i in num_list: heap3.push(i) expected = [-2, 3, 10] assert (expected == [i for i in pop_heap(heap1)]) assert (expected == [i for i in pop_heap(heap2)]) assert (expected == [i for i in pop_heap(heap3)])
def test2(): heap = Heap(max_size) for i in range(5): heap.insert(HeapItem(i, i)) print(heap) heap.build_heap() print(heap)
def random_heap(): h = Heap() h.push(10) h.push(20) h.push(2) h.push(100) h.push(150) return h
def test(): h = Heap() h.data_from_string('SORTEXAMPLE') print 'initial', h s = HeapSort() sorted_heap = s.sort(h) assert unicode(sorted_heap) == 'A E E L M O P R S T X', h print 'sort ok'
def full_heap(): h = Heap() h.push(100) h.push(19) h.push(36) h.push(17) h.push(3) h.push(25) h.push(1) h.push(2) h.push(7) return h
def __init__(self, size, alpha): super(RankBasedReplay, self).__init__(size) assert alpha >= 0 self.alpha = alpha self.sorted = [] self.heap = Heap(self.maxsize) self.max_priority = 1.0 # will change as priorities are updated according to TD error self.N_list, self.range_list = load_quantiles( ) # gets ranges of equal probability of zipf distribution for a few values of N self.range_idx = 0 # index into N_list of the ranges we're currently using self.priority_sums = [ sum([i**(-alpha) for i in range(1, N + 1)]) for N in self.N_list ] # normalizing factors for priority distributions self.min_priorities = [ N**(-alpha) / self.priority_sums[i] for i, N in enumerate(self.N_list) ] # minimum possible priorities given N
from binary_heap import Node from binary_heap import Heap n1 = Node(1) n2 = Node(2) n3 = Node(3) n4 = Node(4) n5 = Node(5) n6 = Node(6) heap = Heap() heap.insert(n1) heap.insert(n2) heap.insert(n3) heap.insert(n4) heap.insert(n5) heap.insert(n6) val = heap.delete() print(str(val.key)) val = heap.delete() print(str(val.key)) val = heap.delete() print(str(val.key))
def empty_heap(): h = Heap() return h
def test_iter_heap(): l = [100, 19, 36, 17, 3, 25, 1, 2, 7] h = Heap(l) assert h.heap_list[1] == 100
def test_heap(): h = Heap() assert h.heap_list[0] == 0
def test_clear_heap(): heap = Heap(num_list) assert (len(heap) == 3) heap.clear() assert (len(heap) == 0) assert (not heap)
def test_copy_heap(): heap = Heap(num_list) copy = heap.copy() assert (heap._vals == copy._vals) assert (heap is not copy)
def test_push_pop(): heap = Heap(num_list) assert (heap.pushpop(0) == -2) assert (heap.pushpop(-1) == -1) assert (heap.poppush(99) == 0) assert (heap.poppush(-10) == 3)
def test_peek_heap(): heap = Heap(num_list) assert (heap.peek() == heap.pop())
def test_max_heap(): heap = Heap(num_list, max_heap=True) expected = [10, 3, -2] assert (expected == [i for i in pop_heap(heap)])