def partition_by_x(head, x): node_more_head = None node_less_tail = None while head != None: #Update the more linkedlist, append to tail. more_head->...more if head.data > x: if not node_more_head: node_more_head = LinkedListNode(head.data) more_head = node_more_head else: node_more_tail = LinkedListNode(head.data) while node_more_head.next != None: node_more_head = node_more_head.next node_more_head.next = node_more_tail print 'more: %s' % more_head.to_str() else: #Update the less linkedlist, update the head. less...->less_tail if not node_less_tail: node_less_tail = LinkedListNode(head.data) less_tail = node_less_tail else: node_less_head = LinkedListNode(head.data) node_less_head.next = node_less_tail node_less_tail = node_less_head print 'less: %s' % node_less_tail.to_str() head = head.next #concatenat. less...->less_tail->more_head->...more less_tail.next = more_head return node_less_tail
def test_search(self): L = LinkedList() a = LinkedListNode(1) b = LinkedListNode(4) c = LinkedListNode(16) d = LinkedListNode(9) e = LinkedListNode(25) L.insert(a) L.insert(b) L.insert(c) L.insert(d) L.insert(e) self.assertEqual(L.search(4), b)
def push(self, value): """Push an item to the top of the stack in O(1)""" if self._size == self._maxsize: raise StackError('Stack is already full') self._size += 1 self._top = LinkedListNode(value, self._top)
def enqueue(self, data): """Add the data to the queue tail.""" new_tail = LinkedListNode(data) if self.tail == None: self.tail = new_tail self.head = new_tail else: self.tail.next = new_tail self.tail = new_tail #Update the tail
def test_insert(self): L = LinkedList() a = LinkedListNode(1) b = LinkedListNode(4) c = LinkedListNode(16) d = LinkedListNode(9) e = LinkedListNode(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_heap_minimum(self): L1 = LinkedList(1) L1.insert(LinkedListNode(1)) L1.insert(LinkedListNode(1)) L2 = LinkedList(2) L2.insert(LinkedListNode(2)) L2.insert(LinkedListNode(2)) L3 = LinkedList(3) L3.insert(LinkedListNode(3)) L3.insert(LinkedListNode(3)) L4 = LinkedList(4) L4.insert(LinkedListNode(4)) L5 = LinkedList(5) L3.insert(LinkedListNode(5)) L3.insert(LinkedListNode(5)) L3.insert(LinkedListNode(5)) q = MinPriorityQueue([L1, L2, L3, L4, L5]) self.assertEqual(q.heap_minimum().key, 1)
def sum_lists_recursive(node_a, node_b, carry=0): if not node_a and not node_b and carry == 0: return None result = LinkedListNode(0) value = carry if node_a: value += node_a.value if node_b: value += node_b.value result.value = value % 10 if node_a or node_b: more = LinkedListNode(sum_lists_recursive( node_a.next if node_a else None, node_b.next if node_b else None, 1 if value >= 10 else 0 )) result.next = more return result
def test_heap_extract_min(self): L1 = LinkedList(1) L1.insert(LinkedListNode(1)) L1.insert(LinkedListNode(1)) L2 = LinkedList(2) L2.insert(LinkedListNode(2)) L2.insert(LinkedListNode(2)) L3 = LinkedList(3) L3.insert(LinkedListNode(3)) L3.insert(LinkedListNode(3)) L4 = LinkedList(4) L4.insert(LinkedListNode(4)) L5 = LinkedList(5) L3.insert(LinkedListNode(5)) L3.insert(LinkedListNode(5)) L3.insert(LinkedListNode(5)) q = min_priority_queue([L1, L2, L3, L4, L5]) self.assertEqual(q.heap_extract_min().key, 1) self.assertEqual(q, [L1, L2, L3, L4, L5]) self.assertEqual(q.heap_extract_min().key, 1) self.assertEqual(q, [L2, L4, L3, L5, L5])
def partition_by_x(head, x): node_more_head = None node_less_tail = None while head != None: # Update the more linkedlist, append to tail. more_head->...more if head.data > x: if not node_more_head: node_more_head = LinkedListNode(head.data) more_head = node_more_head else: node_more_tail = LinkedListNode(head.data) while node_more_head.next != None: node_more_head = node_more_head.next node_more_head.next = node_more_tail print "more: %s" % more_head.to_str() else: # Update the less linkedlist, update the head. less...->less_tail if not node_less_tail: node_less_tail = LinkedListNode(head.data) less_tail = node_less_tail else: node_less_head = LinkedListNode(head.data) node_less_head.next = node_less_tail node_less_tail = node_less_head print "less: %s" % node_less_tail.to_str() head = head.next # concatenat. less...->less_tail->more_head->...more less_tail.next = more_head return node_less_tail
def add_two_linkedlist(node1, node2, followup=False): # 4->2->8->1->9: 91824 4->2->8->1->9: 91824 # 4->2->8->1->9: 91824 4->2->8->1: 1824 # sum: 183648 93648 carry = 0 result_node = None if not followup: while node1 != None or node2 != None: if node2 == None: digit = node1.data + carry if node1 == None: digit = node2.data + carry if node1 != None and node2 != None: digit = node1.data + node2.data + carry carry = 0 # reset carry if digit >= 10: digit = digit % 10 carry = 1 if result_node == None: result_node = LinkedListNode(digit) else: result_node.append_to_tail(digit) if node1 != None: node1 = node1.next if node2 != None: node2 = node2.next if carry != 0: result_node.append_to_tail(carry) return result_node
def test_min_heapify(self): L1 = LinkedList(1) L2 = LinkedList(2) L2.insert(LinkedListNode(2)) L2.insert(LinkedListNode(2)) L3 = LinkedList(3) L3.insert(LinkedListNode(3)) L3.insert(LinkedListNode(3)) L4 = LinkedList(4) L4.insert(LinkedListNode(4)) L5 = LinkedList(5) L3.insert(LinkedListNode(5)) L3.insert(LinkedListNode(5)) L3.insert(LinkedListNode(5)) h = MinHeap([L5, L1, L2, L3, L4]) h.min_heapify(0) self.assertEqual(h, [L1, L3, L2, L5, L4])
def test_build_min_heap(self): L1 = LinkedList(1) L2 = LinkedList(2) L2.insert(LinkedListNode(2)) L2.insert(LinkedListNode(2)) L3 = LinkedList(3) L3.insert(LinkedListNode(3)) L3.insert(LinkedListNode(3)) L4 = LinkedList(4) L4.insert(LinkedListNode(4)) L5 = LinkedList(5) L3.insert(LinkedListNode(5)) L3.insert(LinkedListNode(5)) L3.insert(LinkedListNode(5)) h = min_heap([L3, L4, L5, L2, L1]) h.build_min_heap() self.assertEqual(h, [L1, L2, L5, L3, L4])
def add_two_linkedlist(node1, node2, followup=False): #4->2->8->1->9: 91824 4->2->8->1->9: 91824 #4->2->8->1->9: 91824 4->2->8->1: 1824 #sum: 183648 93648 carry = 0 result_node = None if not followup: while node1 != None or node2 != None: if node2 == None: digit = node1.data + carry if node1 == None: digit = node2.data + carry if node1 != None and node2 != None: digit = node1.data + node2.data + carry carry = 0 # reset carry if digit >= 10: digit = digit % 10 carry = 1 if result_node == None: result_node = LinkedListNode(digit) else: result_node.append_to_tail(digit) if node1 != None: node1 = node1.next if node2 != None: node2 = node2.next if carry != 0: result_node.append_to_tail(carry) return result_node
def push(self, data): """Push data to the stack top. Add new head. """ new_top = LinkedListNode(data) new_top.next = self.top self.top = new_top
return current = ll.head runner = ll.head.next.next while current is not runner: current = current.next.next runner = runner.next return current def loop_detection_pythonic(ll): if ll.tail.next is None: return stack = [] node = ll.head while node not in stack: stack.append(node) node = node.next return node if __name__ == '__main__': loop_node = LinkedListNode(3) ll = LinkedList([1, 2, 0]) ll.tail.next = loop_node ll.tail = ll.tail.next ll.add(4) ll.tail.next = loop_node ll.tail = ll.tail.next print(loop_detection(ll))
longer = ll_a if len(ll_a) > len(ll_b) else ll_b shorter = ll_b if len(ll_b) < len(ll_a) else ll_a s_node, l_node = shorter.head, longer.head diff = len(longer) - len(shorter) for _ in range(diff): l_node = l_node.next while s_node is not l_node: s_node = s_node.next l_node = l_node.next return l_node if __name__ == '__main__': shared_node = LinkedListNode(value=5) ll_a = LinkedList([1, 2, 3, 4]) ll_a.tail.next = shared_node ll_a.tail = ll_a.tail.next ll_b_true = LinkedList([6, 7, 13, 8, 9]) ll_b_true.tail.next = shared_node ll_b_true.tail = ll_b_true.tail.next print(ll_a) print(ll_b_true) #print(intersection_solution(ll_a, ll_b_true)) print(intersection(ll_a, ll_b_true)) ll_b_false = LinkedList([6, 7, 11, 3, 8, 9, 5]) print(ll_b_false)
def setUp(self): head = LinkedListNode('a') head = head.append_to_tail('b') head = head.append_to_tail('b') head = head.append_to_tail('c') other_head = LinkedListNode(4) other_head = other_head.append_to_tail(2) other_head = other_head.append_to_tail(8) other_head = other_head.append_to_tail(1) other_head = other_head.append_to_tail(9) another_head = LinkedListNode(4) another_head = another_head.append_to_tail(2) another_head = another_head.append_to_tail(8) another_head = another_head.append_to_tail(1) loop0 = LinkedListNode('a') loop1 = LinkedListNode(data='b', next=loop0) loop2 = LinkedListNode(data='c', next=loop1) loop3 = LinkedListNode(data='d', next=loop2) loop4 = LinkedListNode(data='e', next=loop3) loop5 = LinkedListNode(data='f', next=loop4) loop0.next = loop3 palindrome_odd = LinkedListNode('a') palindrome_odd = palindrome_odd.append_to_tail('b') palindrome_odd = palindrome_odd.append_to_tail('c') palindrome_odd = palindrome_odd.append_to_tail('b') palindrome_odd = palindrome_odd.append_to_tail('a') palindrome_even = LinkedListNode('a') palindrome_even = palindrome_even.append_to_tail('b') palindrome_even = palindrome_even.append_to_tail('b') palindrome_even = palindrome_even.append_to_tail('a') palindrome = LinkedListNode('a') palindrome = palindrome.append_to_tail('b') palindrome = palindrome.append_to_tail('c') palindrome = palindrome.append_to_tail('d') palindrome = palindrome.append_to_tail('e') self.board = [['x','x','o'], ['o','o','x'], ['x','o','x']] self.board_diag = [['x','x','o'], ['o','x','x'], ['x','o','x']] self.sequences = [1, 2, 4, 7, 10, 11, 7, 12, 6, 7, 16, 18, 19] self.palindrome = palindrome #it's a->b->c->d->e self.palindrome_odd = palindrome_odd #it's a->b->c->b->a self.palindrome_even = palindrome_even #it's a->b->b->a self.loop_head = loop5 #it's f->e->d->c->b->a->d self.head = head #it's a->b->b->c self.other_head = other_head #it's 4->2->8->1->9 self.another_head = another_head #it's 4->2->8->1 self.node = self.head.next.next #it's b. b->c self.node_none = self.head.next.next.next #it's c. c->None self.words = {'Do': 1, 'Here': 1, 'I': 1, 'a': 2, 'book': 2, 'check': 1, 'find': 1, 'for': 1, 'frequencies': 1, 'important': 2, 'in': 2, 'is': 1, 'need': 1, 'testing': 1, 'the': 2, 'this': 1, 'to': 1, 'topics': 1, 'very': 6, 'words': 1}