Beispiel #1
0
def test_ll_includes_false():
    """uses insert() to test includes()"""
    ll = Linked_list()
    ll.insert("apple")
    ll.insert("banana")
    ll.insert("carrot")
    actual = ll.includes("donut")
    expected = False
    assert actual == expected
def test_merge_list_list1_none():
    list1 = Linked_list()
    list2 = Linked_list()
    list2.insert("carrot")
    list2.insert("banana")
    list2.insert("apple")
    actual = str(merge_lists(list1, list2))
    expected = "{ apple } -> { banana } -> { carrot } -> NULL"
    assert actual == expected
Beispiel #3
0
def test_LinkedList_kth_from_end_2():
    """uses insert to test kth_from_end() returns k places away from tail"""
    ll = Linked_list()
    ll.insert(2)
    ll.insert(8)
    ll.insert(3)
    ll.insert(1)
    actual = ll.kth_from_end(2)
    expected = 3
    assert actual == expected
Beispiel #4
0
def test_ll_insert():
    """tests insert() adds node to head"""
    ll = Linked_list()
    ll.insert("apple")
    ll.insert("banana")
    assert ll.head.value == "banana"
    assert ll.head.next.value == 'apple'
Beispiel #5
0
def ll_list():
    """Sets up a linked list instance along with adds 3 nodes for testing "{ carrot } -> { banana } -> { apple } -> NULL""""
    ll = Linked_list()
    ll.insert("apple")
    ll.insert("banana")
    ll.insert("carrot")
    return ll
Beispiel #6
0
def test_LinkedList_str():
    """uses insert() to test str()"""
    ll = Linked_list()
    ll.insert("apple")
    ll.insert("banana")
    actual = str(ll)
    expected = "{ banana } -> { apple } -> NULL"
    assert actual == expected
Beispiel #7
0
def merge_lists( list1, list2):
    """takes two linked lists and merges them starting with head1 then head2"""
    # when one list is shorter 
    if list1.head == None:
        return list2
    if list2.head == None:
        return list1

    list1_current = list1.head
    list2_current = list2.head
    merged = Linked_list()

    #checks is there are indexes available to merge
    while list1_current != None and list2_current != None:

        if list1_current != None:
            if merged.head == None:
                merged.insert(list1_current.value)
            else:
                merged.append(list1_current.value)

        if list2_current != None:
            if merged.head == None:
                merged.insert(list2_current.value)
            else:
                merged.append(list2_current.value)

        if list1_current and list1_current.next:
            list1_current = list1_current.next
        else:
            list1_current = False

        if list2_current and list2_current.next:
            list2_current = list2_current.next
        else:
            list2_current = False
    return merged
Beispiel #8
0
def test_ll_instance():
    assert Linked_list()
Beispiel #9
0
def test_ll_head():
    """uses an empty linked list to test _init_()"""
    ll = Linked_list()
    assert ll.head == None
Beispiel #10
0
def test_ll_repr():
    """uses an empty linked list to test repr()"""
    ll = Linked_list()
    actual = repr(ll)
    expect = "linked list : None"
    assert actual == expect