def test_is_k_longer_than_length(): ll = LinkedList() ll.append_node(1) ll.append_node(2) ll.append_node(3) with pytest.raises(IndexError): ll.find_kth_value_from_end(4)
def zipList(l1, l2): list1 = l1.head list2 = l2.head merged_list = LinkedList() if list1 == None: return list2 if list2 == None: return list1 while list1 is not None and list2 is not None: if list1 is not None and merged_list.head == None: merged_list.insert(list1.data) else: merged_list.append_node(list1.data) if list2 is not None and merged_list.head == None: merged_list.insert(list2.data) else: merged_list.append_node(list2.data) if list1 and list1.next: list1 = list1.next else: list1 = None if list2 and list2.next: list2 = list2.next else: list2 = None return merged_list.head
def test_negative_input(): ll = LinkedList() ll.append_node(1) ll.append_node(2) ll.append_node(3) # test will pass if code in block causes expected ERROR with pytest.raises(ValueError): ll.find_kth_value_from_end(-1)
def test_is_k_same_as_length(): ll = LinkedList() ll.append_node("a") ll.append_node("b") ll.append_node("c") actual = ll.find_kth_value_from_end(2) expected = "a" assert actual == expected
def test_append(): ll = LinkedList() ll.insert(3) ll.insert(2) ll.insert(1) ll.append_node(4) actual = ll.return_all() expected = [1, 2, 3, 4] assert actual == expected
def test_list2_empty(): l1 = LinkedList() l2 = LinkedList() l1.append_node("a") l1.append_node("b") l1.append_node("c") actual = zipList(l1, l2) expected = ["a", "b", "c"] while actual: assert actual.data == expected.pop(0) actual = actual.next
def test_list1_empty(): l1 = LinkedList() l2 = LinkedList() l2.append_node(1) l2.append_node(2) l2.append_node(3) actual = zipList(l1, l2) expected = [1, 2, 3] while actual: assert actual.data == expected.pop(0) actual = actual.next
def test_same_size_lists(): l1 = LinkedList() l2 = LinkedList() l1.append_node("a") l1.append_node("b") l1.append_node("c") l2.append_node(1) l2.append_node(2) l2.append_node(3) actual = zipList(l1, l2) expected = ["a", 1, "b", 2, "c", 3] while actual: assert actual.data == expected.pop(0) actual = actual.next
def test_k_in_middle(): ll = LinkedList() ll.append_node("a") ll.append_node(3) ll.append_node("3") actual = ll.find_kth_value_from_end(1) expected = 3 assert actual == expected
def test_insert_after(): ll = LinkedList() ll.append_node(1) ll.append_node(2) ll.append_node(3) ll.append_node(4) ll.append_node(5) ll.insert_after(3, 3.5) actual = ll.return_all() expected = [1, 2, 3, 3.5, 4, 5] assert actual == expected