def test_merge_list_link2_head(): ll1 = LinkedList() ll2 = LinkedList() ll1.insert("a", "b", "c", "d", "e", "f") actual = str(merge_list(ll1, ll2)) expected = "f : e : d : c : b : a : None" assert actual == expected
def test_LinkedList_str(): ll = LinkedList() ll.insert("a") ll.insert("b") actual = str(ll) expected = "{ b } -> { a } -> NULL" assert actual == expected
def test_merge_list_link1_head(): ll1 = LinkedList() ll2 = LinkedList() ll2.insert("A", "B", "C") actual = str(merge_list(ll1, ll2)) expected = "C : B : A : None" assert actual == expected
def test_filter(): nums = LinkedList() for i in range(1, 21): nums.insert(i) odds = [num for num in nums if num % 2] assert odds == [19, 17, 15, 13, 11, 9, 7, 5, 3, 1]
def test_next(): foods = LinkedList() foods.insert("apple", "banana", "cucumber") iterator = iter(foods) assert next(iterator) == "cucumber" assert next(iterator) == "banana" assert next(iterator) == "apple"
def test_stop_iteration(): foods = LinkedList() foods.insert("apple", "banana", "cucumber") iterator = iter(foods) with pytest.raises(StopIteration): while True: food = next(iterator)
def partition(ll, value): # make a new linked list with a tail to hold high values and head to hold the small values # O(n) time but O(n) for space for new linked list output_ll = LinkedList() output_ll.tail = output_ll.head current = ll.head while current: if current.value < value: if not output_ll.head: output_ll.head = Node(current.value) output_ll.tail = output_ll.head else: new_node = Node(current.value) new_node.next = output_ll.head output_ll.head = new_node else: #add to the tail if not output_ll.tail: output_ll.tail = Node(current.value) output_ll.head = output_ll.tail else: new_node = Node(current.value) output_ll.tail.next = new_node output_ll.tail = new_node current = current.next return output_ll
def test_merge_lists_same_length(): ll1 = LinkedList() ll2 = LinkedList() ll1.insert("a", "b", "c") ll2.insert("A", "B", "C") actual = str(merge_list(ll1, ll2)) expected = 'c : C : b : B : a : A : None' assert actual == expected
def test_merge_list_link2_long(): ll1 = LinkedList() ll2 = LinkedList() ll1.insert("a", "b", "c") ll2.insert("A", "B", "C", "D", "E", "F") actual = str(merge_list(ll1, ll2)) expected = "c : F : b : E : a : D : C : B : A : None" assert actual == expected
def test_merge_list_link1_long(): ll1 = LinkedList() ll2 = LinkedList() ll1.insert("a", "b", "c", "d", "e", "f") ll2.insert("A", "B", "C") actual = str(merge_list(ll1, ll2)) expected = "f : C : e : B : d : A : c : b : a : None" assert actual == expected
def test_equals(): lla = LinkedList() lla.insert("apple", "banana", "cucumber") llb = LinkedList() llb.insert("apple", "banana", "cucumber") assert len(lla) == len(llb) assert str(lla) == str(llb)
def sum_lists2(ll_1, ll_2): if not ll_1.head or not ll_2.head: return "ll does not have head" ll = LinkedList() current_1 = ll_1.head current_2 = ll_2.head carry_over = 0 def get_new_value(sum, carry_over): if sum > 9: new_value = sum - 10 carry_over = 1 else: new_value = sum carry_over = 0 return new_value, carry_over def add_to_tail(new_value): ll.tail.next = Node(new_value) ll.tail = ll.tail.next while current_1 and current_2: sum = current_1.value + current_2.value + carry_over new_value, carry_over = get_new_value(sum, carry_over) if not ll.head: ll.head = Node(new_value) ll.tail = ll.head else: add_to_tail(new_value) current_1 = current_1.next current_2 = current_2.next if current_1: remaining = current_1 else: remaining = current_2 while remaining: sum = remaining.value + carry_over new_value, carry_over = get_new_value(sum, carry_over) add_to_tail(new_value) remaining = remaining.next if carry_over: ll.tail.next = Node(carry_over) ll.tail = ll.tail.next return ll
def test_LinkedList_includes_false(): ll = LinkedList() ll.insert("a") ll.insert("b") ll.insert("c") actual = ll.includes("d") expected = False assert actual == expected
def test_LinkedList_includes_true(): ll = LinkedList() ll.insert("a") ll.insert("b") ll.insert("c") actual = ll.includes("c") expected = True assert actual == expected
def test_LinkedList_append_empty_list(): ll = LinkedList() ll.insert("1", "3", "2") ll.append("5") actual = str(ll) expected = "{2} -> {3} -> {1} -> {5} -> NULL" assert actual == expected
def test_LinkedList_insert_after_multiple_arg_single_link(): ll = LinkedList() ll.insert("apples") ll.insert_after("apples", "1", "2", "3") actual = str(ll) expected = "{apples} -> {3} -> {2} -> {1} -> NULL" assert actual == expected
def test_LinkedList_insert_before_present_value(): ll = LinkedList() ll.insert("oranges", "bananas", "coconut") ll.insert_before("oranges", "kiwi") actual = str(ll) expected = "{coconut} -> {bananas} -> {kiwi} -> {oranges} -> NULL" assert actual == expected
def test_LinkedList_insert_after_multiple_arg_large_link(): ll = LinkedList() ll.insert("apples", "kiwi", "coconut", "pineapple") ll.insert_after("kiwi", "1", "2", "3") actual = str(ll) expected = "{pineapple} -> {coconut} -> {kiwi} -> {3} -> {2} -> {1} -> {apples} -> NULL" assert actual == expected
def test_LinkedList_insert_after_single_list(): ll = LinkedList() ll.insert("apples") ll.insert_after("apples", "1") actual = str(ll) expected = "{apples} -> {1} -> NULL" assert actual == expected
def test_LinkedList_insert_after_single_new_value(): ll = LinkedList() ll.insert("oranges", "kiwi", "bananas") ll.insert_after("kiwi", "1") actual = str(ll) expected = "{bananas} -> {kiwi} -> {1} -> {oranges} -> NULL" assert actual == expected
def test_LinkedList_insert_before_multiple_arg_long_head(): ll = LinkedList() ll.insert("oranges", "kiwi", "bananas", "coconut") ll.insert_before("kiwi", "1", "2", "3") actual = str(ll) expected = "{coconut} -> {bananas} -> {3} -> {2} -> {1} -> {kiwi} -> {oranges} -> NULL" assert actual == expected
def test_LinkedList_insert_before_multiple_arg_just_head(): ll = LinkedList() ll.insert("oranges") ll.insert_before("oranges", "1", "2", "3") actual = str(ll) expected = "{3} -> {2} -> {1} -> {oranges} -> NULL" assert actual == expected
def sum_lists(ll_1, ll_2): if not ll_1.head or not ll_2.head: return "ll does not have head" ll = LinkedList() current_1 = ll_1.head current_2 = ll_2.head carry_over = 0 while current_1 and current_2: sum = current_1.value + current_2.value + carry_over if sum > 9: new_value = sum - 10 carry_over = 1 else: new_value = sum carry_over = 0 if not ll.head: ll.head = Node(new_value) ll.tail = ll.head else: ll.tail.next = Node(new_value) ll.tail = ll.tail.next current_1 = current_1.next current_2 = current_2.next while current_1: sum = current_1.value + carry_over if sum > 9: new_value = sum - 10 carry_over = 1 else: new_value = sum carry_over = 0 ll.tail.next = Node(new_value) ll.tail = ll.tail.next current_1 = current_1.next while current_2: sum = current_2.value + carry_over if sum > 9: new_value = sum - 10 carry_over = 1 else: new_value = sum carry_over = 0 ll.tail.next = Node(new_value) ll.tail = ll.tail.next current_2 = current_2.next if carry_over: ll.tail.next = Node(carry_over) ll.tail = ll.tail.next return ll
def test_LinkedList_append_empty_list(): ll = LinkedList() ll.append("5") actual = str(ll) expected = "{5} -> NULL" assert actual == expected
def test_LinkedList_instantiation(): ll = LinkedList() actual = ll.head expected = None assert actual == expected
def ll(): ll = LinkedList() ll.insert("apples") return ll
def test_kth_value_one(): ll = LinkedList() ll.insert("1") actual = ll.find_k_node_value2(0) expected = '1' assert actual == expected
def test_kth_value_mid_length(): ll = LinkedList() ll.insert("1", "2", "3", "4", "5") actual = ll.find_k_node_value2(2) expected = '3' assert actual == expected
def test_kth_value_negativeKay(): ll = LinkedList() ll.insert("1", "2", "3") actual = ll.find_k_node_value2(-15) expected = "Exception" assert actual == expected
def test_kth_value_empty_list(): ll = LinkedList() actual = ll.find_k_node_value2(3) expected = "Exception" assert actual == expected