def test_insert_nth(): inputlist = build_ll_from_lst([1,2,4]) answerlist = build_ll_from_lst([1,2,3,4]) outputlist = insert_nth(inputlist.head, 3,2) assert answerlist.lists_eq(outputlist) inputlist = build_ll_from_lst([1,2,4]) answerlist = build_ll_from_lst([0,1,2,4]) outputlist = insert_nth(inputlist.head, 0,0) assert answerlist.lists_eq(outputlist)
def test_compare_lists_iterative(): llA = build_ll_from_lst([2, 4]) llB = build_ll_from_lst([2, 4]) headA = llA.head headB = llB.head assert compare_lists_iterative(headA, headB) llA = build_ll_from_lst([2]) llB = build_ll_from_lst([2, 4]) headA = llA.head headB = llB.head assert not compare_lists_iterative(headA, headB)
def test_get_max_value_in_list_recursive(): l1 = build_ll_from_lst([1,2,3]) node = l1.head assert get_max_value_in_list_recursive(node) == 3 l1 = build_ll_from_lst([1]) node = l1.head assert get_max_value_in_list_recursive(node) == 1 l1 = build_ll_from_lst([1,3]) node = l1.head.get_next() assert get_max_value_in_list_recursive(node) == 3
def test_reverse_linked_list_recursive(): inputlist = build_ll_from_lst([1, 2, 3]) head1 = inputlist.head outputnode = reverse_linked_list_recursive(head1, None) assert outputnode.value == 3 outputnode = outputnode.next assert outputnode.value == 2 outputnode = outputnode.next assert outputnode.value == 1 outputnode = outputnode.next assert outputnode is None inputlist = build_ll_from_lst([1]) head1 = inputlist.head outputnode = reverse_linked_list_recursive(head1, None) assert outputnode.value == 1 outputnode = outputnode.next assert outputnode is None
def test_reverse_linked_list_recursive(): inputlist = build_ll_from_lst([1,2,3]) head1 = inputlist.head outputnode = reverse_linked_list_recursive(head1, None) assert outputnode.value == 3 outputnode = outputnode.next assert outputnode.value == 2 outputnode = outputnode.next assert outputnode.value == 1 outputnode = outputnode.next assert outputnode is None inputlist = build_ll_from_lst([1]) head1 = inputlist.head outputnode = reverse_linked_list_recursive(head1, None) assert outputnode.value == 1 outputnode = outputnode.next assert outputnode is None
def test_remove_duplicates_unsorted(): l1 = build_ll_from_lst([1,2,4,2,1,3,7,3,4]) l2 = build_ll_from_lst([1,1,1,1,1,1,1]) l3 = build_ll_from_lst([6,2,4,3,3,4,6]) l4 = build_ll_from_lst([10]) a1 = build_ll_from_lst([1,2,4,3,7]) a2 = build_ll_from_lst([1]) a3 = build_ll_from_lst([6,2,4,3]) a4 = build_ll_from_lst([10]) assert remove_duplicates_unsorted(l1.head).lists_eq(a1) assert remove_duplicates_unsorted(l2.head).lists_eq(a2) assert remove_duplicates_unsorted(l3.head).lists_eq(a3) assert remove_duplicates_unsorted(l4.head).lists_eq(a4)
def test_sum_two_lists_proper_string(): list1 = build_ll_from_lst([]) list2 = build_ll_from_lst([]) assert sum_two_lists_proper_string(list1,list2) == 0 list1 = build_ll_from_lst([1]) list2 = build_ll_from_lst([1]) assert sum_two_lists_proper_string(list1,list2) == 2 list1 = build_ll_from_lst([5,3,1]) list2 = build_ll_from_lst([5,2]) assert sum_two_lists_proper_string(list1,list2) == 583
def test_sum_two_lists(): list1 = build_ll_from_lst([]) list2 = build_ll_from_lst([]) assert sum_two_lists(list1,list2) == 0 list1 = build_ll_from_lst([1]) list2 = build_ll_from_lst([1]) assert sum_two_lists(list1,list2) == 2 list1 = build_ll_from_lst([5,3,1]) list2 = build_ll_from_lst([5,2]) assert sum_two_lists(list1,list2) == 160
def test_sum_two_lists_return_list(): list1 = build_ll_from_lst([]) list2 = build_ll_from_lst([]) outputlist = sum_two_lists_return_list(list1,list2) print outputlist.get_linked_list_str() answerlist = build_ll_from_lst([None]) print answerlist.get_linked_list_str() assert answerlist.lists_eq(outputlist) == True list1 = build_ll_from_lst([1]) list2 = build_ll_from_lst([1]) outputlist = sum_two_lists_return_list(list1,list2) print outputlist.get_linked_list_str() answerlist = build_ll_from_lst([2,None]) assert answerlist.lists_eq(outputlist) == True list1 = build_ll_from_lst([5,3,1]) list2 = build_ll_from_lst([5,2]) outputlist = sum_two_lists_return_list(list1,list2) print outputlist.get_linked_list_str() answerlist = build_ll_from_lst([0,6,1,None]) print answerlist.get_linked_list_str() assert answerlist.lists_eq(outputlist) == True
def test_reverse_linked_list_iterative(): inputlist = build_ll_from_lst([1, 2, 3]) answerlist = build_ll_from_lst([3, 2, 1]) outputlist = reverse_linked_list_iterative(inputlist) assert answerlist.lists_eq(outputlist) inputlist = build_ll_from_lst([1]) answerlist = build_ll_from_lst([1]) outputlist = reverse_linked_list_iterative(inputlist) assert answerlist.lists_eq(outputlist) inputlist = build_ll_from_lst([]) answerlist = build_ll_from_lst([]) outputlist = reverse_linked_list_iterative(inputlist) assert answerlist.lists_eq(outputlist)
def test_reverse_linked_list_iterative(): inputlist = build_ll_from_lst([1,2,3]) answerlist = build_ll_from_lst([3,2,1]) outputlist = reverse_linked_list_iterative(inputlist) assert answerlist.lists_eq(outputlist) inputlist = build_ll_from_lst([1]) answerlist = build_ll_from_lst([1]) outputlist = reverse_linked_list_iterative(inputlist) assert answerlist.lists_eq(outputlist) inputlist = build_ll_from_lst([]) answerlist = build_ll_from_lst([]) outputlist = reverse_linked_list_iterative(inputlist) assert answerlist.lists_eq(outputlist)
def test_get_nth_node_from_tail(): l1 = build_ll_from_lst([1,2,3]) node = l1.head assert get_nth_node_from_tail(node,0) == 3 assert get_nth_node_from_tail(node,1) == 2 assert get_nth_node_from_tail(node,2) == 1
before_slow = head slow = head fast = head while fast != None: fast = fast.next if fast != None: before_slow = slow fast = fast.next slow = slow.next before_slow.next = None return slow ## Test Get Middle Node t0 = build_ll_from_lst([5,3,6,2]) print get_middle_node(t0.head).value == 6 ## Test Merge t1 = build_ll_from_lst([1,3,4,6]).head t2 = build_ll_from_lst([2,3,5]).head print LinkedList(merge(t1,t2)).get_linked_list_str() ## Test Sort t4 = build_ll_from_lst([5,3,6,2,14,7,2,3]) t5 = build_ll_from_lst([3,2,1]) t6 = build_ll_from_lst([3,2,2,3]) t7 = build_ll_from_lst([1]) t8 = build_ll_from_lst([4,2,3,1,6,3,2]) print LinkedList(sort(t4.head)).get_linked_list_str()