def test_forward(self): for test_case in self.test_cases_reverse: number1_ll = LinkedList(initial_members=reversed(test_case[0])) number2_ll = LinkedList(initial_members=reversed(test_case[1])) sum_ll = LinkedList() solution2(number1_ll.head, number2_ll.head, sum_ll, 0) self.assertEqual(sum_ll.values(), test_case[2])
def test_reverse(self): for test_case in self.test_cases_reverse: number1_ll = LinkedList(initial_members=test_case[0]) number2_ll = LinkedList(initial_members=test_case[1]) sum_ll = LinkedList() solution1(number1_ll, number2_ll, sum_ll) self.assertEqual(sum_ll.values(), test_case[2])
class TestClass(unittest.TestCase): shared_ll = LinkedList(initial_members=[10,20,30]) a = LinkedList(initial_members=[1,2,3,4,5,6]) b = LinkedList(initial_members=[-1,-2,-3]) a_current = a.head.next while a_current.next.data!=None: a_current = a_current.next a_current.next = shared_ll.head.next a.tail = shared_ll.tail b_current = b.head.next while b_current.next.data!=None: b_current = b_current.next b_current.next = shared_ll.head.next b.tail = shared_ll.tail test_cases = [ (a, b, 10) ] test_functions = [solution1] def test_template(self): for test_function in self.test_functions: for t_input_1, t_input_2, e_output in self.test_cases: self.assertEqual(test_function(t_input_1, t_input_2), e_output)
def solution1(ll, pivot): """ Time Complexity: O(n) where n is the length of the linked list Space Complexity: O(n) Args: ll (LinkedList): linked list object pivot (int): Pivot for partition Returns: ll (LinkedList): linked list object with partitioned numbers. """ # Note that ll implementation uses sentinel head and tail node # In the solution, no other information than linked list head is used! before_list = LinkedList() before_list_current = before_list.head after_list = LinkedList() current_node = ll.head.next while current_node.data != None: if current_node.data < pivot: before_list.append(current_node.data) before_list_current = before_list_current.next else: after_list.append(current_node.data) current_node = current_node.next before_list_current.next = after_list.head.next return before_list
class TestClass(unittest.TestCase): test_ll_1 = LinkedList(initial_members=[1, 2]) dup_node = test_ll_1.append(value=3) _ = test_ll_1.append(value=4) last_node = test_ll_1.append(5) last_node.next = dup_node test_ll_2 = LinkedList(initial_members=[1, 2, 3, 4, 5, 6, 7, 8]) test_cases = [(test_ll_1, 3), (test_ll_2, -1)] test_functions = [solution1, solution2] def test_template(self): for test_function in self.test_functions: for t_input, e_output in self.test_cases: self.assertEqual(test_function(t_input), e_output)
def test_template(self): for test_function in self.test_functions: for t_input, k, e_output in self.test_cases: input_ll = LinkedList(initial_members=t_input) p_out = solution1(input_ll, k) self.assertEqual(p_out, e_output)
def test_template(self): for test_function in self.test_functions: for t_input, e_output in self.test_cases: input_ll = LinkedList(initial_members=t_input) processed_ll = solution1(input_ll) self.assertEqual(processed_ll.values(), e_output)
def test_template(self): input_ll = LinkedList(initial_members=[10,20,30,40]) node_to_be_deleted = input_ll.append(value=0) input_ll.appendmultiple(values=[50,60,70]) solution1(node_to_be_deleted) self.assertEqual(input_ll.values(), [10,20,30,40,50,60,70])