def test_search(self): L = linked_list() a = linked_list_node(1) b = linked_list_node(4) c = linked_list_node(16) d = linked_list_node(9) e = linked_list_node(25) L.insert(a) L.insert(b) L.insert(c) L.insert(d) L.insert(e) self.assertEqual(L.search(4), b)
def test_search(self): L = linked_list() a = linked_list_node(1) b = linked_list_node(4) c = linked_list_node(16) d = linked_list_node(9) e = linked_list_node(25) L.insert(a) L.insert(b) L.insert(c) L.insert(d) L.insert(e) self.assertEquals(L.search(4), b)
def test_heap_minimum(self): L1 = linked_list(1) L1.insert(linked_list_node(1)) L1.insert(linked_list_node(1)) L2 = linked_list(2) L2.insert(linked_list_node(2)) L2.insert(linked_list_node(2)) L3 = linked_list(3) L3.insert(linked_list_node(3)) L3.insert(linked_list_node(3)) L4 = linked_list(4) L4.insert(linked_list_node(4)) L5 = linked_list(5) L3.insert(linked_list_node(5)) L3.insert(linked_list_node(5)) L3.insert(linked_list_node(5)) q = min_priority_queue([L1, L2, L3, L4, L5]) self.assertEquals(q.heap_minimum().key, 1)
def test_insert(self): L = linked_list() a = linked_list_node(1) b = linked_list_node(4) c = linked_list_node(16) d = linked_list_node(9) e = linked_list_node(25) L.insert(a) L.insert(b) L.insert(c) L.insert(d) L.insert(e) l = [] x = L.head while x != None: l.append(x) x = x.next self.assertEquals(l, [e, d, c, b, a])
def test_insert(self): L = linked_list() a = linked_list_node(1) b = linked_list_node(4) c = linked_list_node(16) d = linked_list_node(9) e = linked_list_node(25) L.insert(a) L.insert(b) L.insert(c) L.insert(d) L.insert(e) l = [] x = L.head while x: l.append(x) x = x.next self.assertEqual(l, [e, d, c, b, a])
def test_delete(self): L = linked_list() a = linked_list_node(1) b = linked_list_node(4) c = linked_list_node(16) d = linked_list_node(9) e = linked_list_node(25) L.insert(a) L.insert(b) L.insert(c) L.insert(d) L.insert(e) L.delete(b) l = [] x = L.head while x != None: l.append(x) x = x.next self.assertEquals(l, [e, d, c, a])
def remove_dups(node): ref = set() prev = linked_list_node() current = node while (current is not None): if current.value in ref: prev.next_node = current.next_node else: ref.add(current.value) prev = current current = current.next_node return node
def test_min_heapify(self): L1 = linked_list(1) L2 = linked_list(2) L2.insert(linked_list_node(2)) L2.insert(linked_list_node(2)) L3 = linked_list(3) L3.insert(linked_list_node(3)) L3.insert(linked_list_node(3)) L4 = linked_list(4) L4.insert(linked_list_node(4)) L5 = linked_list(5) L3.insert(linked_list_node(5)) L3.insert(linked_list_node(5)) L3.insert(linked_list_node(5)) h = min_heap([L5, L1, L2, L3, L4]) h.min_heapify(0) self.assertEquals(h, [L1, L3, L2, L5, L4])
def test_build_min_heap(self): L1 = linked_list(1) L2 = linked_list(2) L2.insert(linked_list_node(2)) L2.insert(linked_list_node(2)) L3 = linked_list(3) L3.insert(linked_list_node(3)) L3.insert(linked_list_node(3)) L4 = linked_list(4) L4.insert(linked_list_node(4)) L5 = linked_list(5) L3.insert(linked_list_node(5)) L3.insert(linked_list_node(5)) L3.insert(linked_list_node(5)) h = min_heap([L3, L4, L5, L2, L1]) h.build_min_heap() self.assertEqual(h, [L1, L2, L5, L3, L4])
def party(node, x): smaller = linked_list_node() bigger = linked_list_node() while node is not None: if node.value < x: smaller.append(linked_list_node(node.value)) else: bigger.append(linked_list_node(node.value)) node = node.next_node # I know. I'm lazy today. Gimme a break. smaller.pop_head() bigger.pop_head() print "Smaller: " + str(smaller) print "Bigger : " + str(bigger) current = smaller while current.next_node is not None: current = current.next_node current.next_node = bigger return smaller