def bst_sequences(root): """ This is a Python implementation of Gayle's solution from CtCI. I struggled with this problem so I just want to move past it. """ result = LinkedList() if root is None: result.insert(LinkedList()) return result prefix = LinkedList() prefix.add(root.data) #Recurse on left and right subtrees left_sequences = bst_sequences(root.left) right_sequences = bst_sequences(root.right) #Weave together each list from the left and right sides. while not left_sequences.is_empty(): left = left_sequences.get_first().data while not right_sequences.is_empty(): right = right_sequences.get_first().data weaved = LinkedList() weave_lists(left, right, weaved, prefix) result.add_all(weaved) return result
def list_of_depths(root): """ This solution is a modification of breadth first search. """ q = MyQueue() depth_lists = [] seen = [] q.add((root, 0)) seen.append(root) while not q.is_empty(): q.print_queue() node, depth = q.remove().data try: depth_lists[depth].insert(node) except IndexError: depth_lists.append(LinkedList()) depth_lists[depth].insert(node) adjacent_nodes = [node.left, node.right] for child in adjacent_nodes: if child is not None and child not in seen: seen.append(child) q.add((child, depth + 1)) return depth_lists
def test_sum_lists(num_a, num_b): a = LinkedList() a_str = str(num_a) for digit in a_str: a.insert(int(digit)) b = LinkedList() b_str = str(num_b) for digit in b_str: b.insert(int(digit)) a.print_list() b.print_list() result = sum_lists(a, b) result.print_list()
def sum_lists(a, b): a_num = 0 a_mult = 1 a_curr = a.head while a_curr is not None: a_num += a_curr.data * a_mult a_curr = a_curr._next a_mult *= 10 b_num = 0 b_mult = 1 b_curr = b.head while b_curr is not None: b_num += b_curr.data * b_mult b_curr = b_curr._next b_mult *= 10 output_num = a_num + b_num print(output_num) output_mult = 1 while output_num // (output_mult * 10) > 0: output_mult *= 10 output = LinkedList() if output_num == 0: output.insert(0) return output while output_num != 0: digit = int(output_num // output_mult) output.insert(digit) output_num = output_num - (digit * output_mult) output_mult /= 10 return output
def test_palindrome(): a = ['aabbaa', 'catac', 'ababab'] for x in a: print(x) li = LinkedList() li.build_from_collection(x) li.print_list() print('Is a palindrome:', is_a_palindrome(li))
def test_partition(): a = [3, 5, 8, 5, 10, 2, 1] li = LinkedList() li.build_from_collection(a) li.print_list() partition(li, 5) li.print_list()
def test_weave(): a = ['a1', 'a2', 'a3', 'a4', 'b1', 'b2', 'b3', 'b4'] print(a) li = LinkedList() li.build_from_collection(a) li.print_list() weave(li) li.print_list()
def test_delete_middle_node(): a = ['a', 'b', 'c', 'd', 'e', 'f'] li = LinkedList() li.build_from_collection(a) li.print_list() middle_node = kth_to_last(li, 3) delete_middle_node(middle_node) li.print_list()
def test_loop_detection(): li = LinkedList() li.insert('C') li.insert('E') li.insert('D') c_node = li.head._next._next c_node._next = li.head li.head = c_node #Oops now we have a loop. li.insert('B') li.insert('A') loop_node = loop_detection(li) if loop_node is not None: print('Has loop at node %s.' % loop_node.data) else: print('Contains no loop.')
def main(): n = -1 while n < 0: try: n = int(input('Введите n: ')) if n < 0: raise ValueError except ValueError: print('Нужно вводить целое положительное число!') l = LinkedList() fac_sum = 0 cur_fac = 1 for i in range(1, n + 1): cur_fac *= i fac_sum += 1 / cur_fac l.add(i * fac_sum) for i in l.list(): print('%.2f' % i)
def is_a_palindrome(li): """ This solution relies on the fact that the linked list implementation inserts nodes at the head of the list. It first scans the list to count the number of elements. Then we pop off the first half of the list while inserting into a new list. This will reverse the elements in a stack-like manner. If the original list contains an odd number of elements we discard the middle element. Finally, we check to see if the two lists we're left with match. This solution destroyes the original list, we could easily add a step to copy it into a new list first to avoid destroying it. """ n = 0 curr = li.head while curr is not None: n += 1 curr = curr._next li2 = LinkedList() k = n // 2 while k > 0: popped_node = li.get_first() li2.insert(popped_node.data) k -= 1 if n % 2 == 1: #Remove the middle element and discard it. _ = li.get_first() while not li2.is_empty(): li2_el = li2.get_first() li_el = li.get_first() if li2_el.data != li_el.data: return False return True
def setUp(self): self.linked_list = LinkedList()
def insert(self, val): index = self._hash(val) if self.table[index] is None: _list = LinkedList() self.table[index] = _list self.table[index].insert(val)