def test_problem_2_3(self): n1 = ll.Node(1, None) n2 = ll.Node(2, n1) n10 = ll.Node(10, n2) n5 = ll.Node(5, n10) string_representation = '' head = n5 p_2_3.delete_middle(n10) while head is not None: string_representation += str(head.value) head = head.next_node self.assertEqual(string_representation, '521')
def test_problem_2_1_alternative(self): n0 = ll.Node(9, None) n1 = ll.Node(7, n0) n2 = ll.Node(2, n1) n3 = ll.Node(5, n2) n4 = ll.Node(3, n3) n5 = ll.Node(3, n4) n6 = ll.Node(7, n5) self.assertEqual('7,3,3,5,2,7,9,', ll.stringify_linked_list(n6)) p_2_1.remove_dups_alternative(n6) self.assertEqual('7,3,5,2,9,', ll.stringify_linked_list(n6))
def test_problem_2_5(self): """ List1: 3 -> 2 -> 4 -> 9 -> NONE List2: 1 -> 5 -> 9 -> NONE 9423 + 951 ----- 10374 """ # first list n14 = ll.Node(9, None) n13 = ll.Node(4, n14) n12 = ll.Node(2, n13) n11 = ll.Node(3, n12) # second list n23 = ll.Node(9, None) n22 = ll.Node(5, n23) n21 = ll.Node(1, n22) sum_head = p_2_5.sum_lists(n11, n21) list_num = "" while sum_head is not None: list_num = str( sum_head.value) + list_num # careful to reverse order! sum_head = sum_head.next_node self.assertEqual(list_num, '10374')
def test_problem_2_2(self): n0 = ll.Node(9, None) n1 = ll.Node(7, n0) n2 = ll.Node(2, n1) n3 = ll.Node(5, n2) n4 = ll.Node(3, n3) n5 = ll.Node(3, n4) n6 = ll.Node(7, n5) self.assertEqual(n2, p_2_2.return_kth_to_last(n6, 3)) self.assertEqual(n5, p_2_2.return_kth_to_last(n6, 6)) self.assertEqual(None, p_2_2.return_kth_to_last(n6, 0))
def test_problem_2_4(self): n1 = ll.Node(1, None) n2 = ll.Node(2, n1) n10 = ll.Node(10, n2) n5_1 = ll.Node(5, n10) n8 = ll.Node(8, n5_1) n5_0 = ll.Node(5, n8) n3 = ll.Node(3, n5_0) new_head = p_2_4.partition(n3, 5) string_representation = '' while new_head is not None: string_representation += str(new_head.value) new_head = new_head.next_node self.assertEqual(string_representation, '12358510')
def test_problem_2_8(self): # see problem_2_8_explanation.pdf # example #1 node1_7 = ll.Node(7, None) node1_6 = ll.Node(6, node1_7) node1_5 = ll.Node(5, node1_6) node1_4 = ll.Node(4, node1_5) node1_3 = ll.Node(3, node1_4) node1_2 = ll.Node(2, node1_3) node1_1 = ll.Node(1, node1_2) node1_0 = ll.Node(0, node1_1) node1_7.next_node = node1_5 self.assertEqual(p_2_8.find_loop(node1_0), node1_5) # example #2 node2_10 = ll.Node(7, None) node2_9 = ll.Node(7, node2_10) node2_8 = ll.Node(7, node2_9) node2_7 = ll.Node(7, node2_8) node2_6 = ll.Node(6, node2_7) node2_5 = ll.Node(5, node2_6) node2_4 = ll.Node(4, node2_5) node2_3 = ll.Node(3, node2_4) node2_2 = ll.Node(2, node2_3) node2_1 = ll.Node(1, node2_2) node2_0 = ll.Node(0, node2_1) node2_10.next_node = node2_3 self.assertEqual(p_2_8.find_loop(node2_0), node2_3)
def test_problem_2_7(self): # list 1 node1_7 = ll.Node(7, None) node1_6 = ll.Node(6, node1_7) node1_5 = ll.Node(5, node1_6) node1_4 = ll.Node(4, node1_5) node1_3 = ll.Node(3, node1_4) node1_2 = ll.Node(2, node1_3) node1_1 = ll.Node(1, node1_2) node1_0 = ll.Node(0, node1_1) # list 2 node2_5 = ll.Node(5, node1_6) # intersection point node2_4 = ll.Node(4, node2_5) node2_3 = ll.Node(3, node2_4) node2_2 = ll.Node(2, node2_3) self.assertEqual(node1_6, p_2_7.intersection(node2_2, node1_0))
def test_problem_2_6(self): # list 1 node1_7 = ll.Node(0, None) node1_6 = ll.Node(4, node1_7) node1_5 = ll.Node(7, node1_6) node1_4 = ll.Node(0, node1_5) node1_3 = ll.Node(0, node1_4) node1_2 = ll.Node(7, node1_3) node1_1 = ll.Node(4, node1_2) node1_0 = ll.Node(0, node1_1) # list 2 node2_4 = ll.Node(3, None) node2_3 = ll.Node(5, node2_4) node2_2 = ll.Node(2, node2_3) node2_1 = ll.Node(5, node2_2) node2_0 = ll.Node(3, node2_1) # list 3 node3_5 = ll.Node(1, None) node3_4 = ll.Node(0, node3_5) node3_3 = ll.Node(1, node3_4) node3_2 = ll.Node(0, node3_3) node3_1 = ll.Node(1, node3_2) node3_0 = ll.Node(0, node3_1) self.assertTrue(p_2_6.palindrome(node1_0)) self.assertTrue(p_2_6.palindrome(node2_0)) self.assertFalse(p_2_6.palindrome(node3_0))
def test_problem_2_7(self): # list 1 node1_6 = ll.Node(1, None) node1_5 = ll.Node(2, node1_6) node1_4 = ll.Node(7, node1_5) node1_3 = ll.Node(9, node1_4) node1_2 = ll.Node(5, node1_3) node1_1 = ll.Node(1, node1_2) node1_0 = ll.Node(3, node1_1) # list 2 node2_1 = ll.Node(6, node1_4) # intersection point node2_0 = ll.Node(4, node2_1) # list 3 node3_1 = ll.Node(6, None) node3_0 = ll.Node(4, node3_1) self.assertEqual(node1_4, p_2_7.intersection(node2_0, node1_0)) self.assertEqual(None, p_2_7.intersection(node1_0, node3_0)) self.assertEqual(None, p_2_7.intersection(None, None))