def test_zipped_list():
    ll1 = LinkedList(Node('1', Node('3', Node('5'))))
    ll2 = LinkedList(Node('2', Node('4', Node('6'))))
    zipped_list(ll1, ll2)
    actual = ll1.__str__()
    expected = "{'1'} ->{'2'} ->{'3'} ->{'4'} ->{'5'} ->{'6'} -> None "
    assert actual == expected
def test_zip_long_list():
    max = 100
    ll_primary = LinkedList([even for even in range(0, max, 2)])
    ll_secondary = LinkedList([odd for odd in range(1, max, 2)])
    ll_primary.zip(ll_secondary)
    ll_check = LinkedList([x for x in range(0, max)])
    print(str(ll_primary))
    assert str(ll_primary) == str(ll_check)
def test_list1_longer():
    list1 = LinkedList()
    list1.append(1)
    list1.append(2)
    list1.append(3)
    list2 = LinkedList()
    list2.append(4)
    list2.append(5)
    actual = str(zipLists(list1, list2))
    expected = "{1}->{4}->{2}->{5}->{3}->NULL"
    assert actual == expected
def test_zip():
    lista = LinkedList()
    listb = LinkedList()
    lista.append_item('A')
    lista.append_item('B')
    listb.append_item('1')
    listb.append_item('2')
    answer = lista.zip_list(listb)
    actual = answer.find_all()
    expected = ['A', '1', 'B', '2']
    assert actual == expected
def test_list1_shorter_plus():
    list1 = LinkedList()
    list1.append(1)
    list1.append(2)
    list2 = LinkedList()
    list2.append(4)
    list2.append(5)
    list2.append(6)
    list2.append(7)
    list2.append(8)
    actual = str(zipLists(list1, list2))
    expected = "{1}->{4}->{2}->{5}->{6}->{7}->{8}->NULL"
    assert actual == expected
def test_includes():
    ll = LinkedList()
    ll.insert('first')
    assert ll.includes("second") is False
    assert ll.includes("first") is True
    ll.insert("another")
    assert ll.includes("another") is True
def test_insert_after_does_not_exist():
    ll = LinkedList()
    ll.insert(1)
    ll.insert(2)
    with pytest.raises(Exception) as context:
        ll.insert_after(3, 3)
    assert str(context.value) == "No node containing: 3"
def test_append_several():
    ll = LinkedList()
    ll.append(1)
    ll.append(2)
    ll.append(3)
    assert ll.includes(3)
    assert str(ll) == '{ 1 } -> { 2 } -> { 3 } -> NULL'
def test_two():
    ll2 = LinkedList()
    ll2.insert("a")
    ll2.insert("b")
    actual = ll2.head.value
    expected = "b"
    assert actual == expected
def test_to_string(empty_list):
    ll = LinkedList()
    assert str(ll) == "NULL"
    ll.insert('3')
    ll.insert(2)
    ll.insert('1')
    assert str(ll) == '{ 1 } -> { 2 } -> { 3 } -> NULL'
def test_linked_list_str():
    link_list = LinkedList()
    link_list.insert('c')
    link_list.insert('b')
    link_list.insert('a')
    actual = str(link_list)
    expected = "{a}->{b}->{c}->NULL"
    assert actual == expected
def test_to_string():
    list = LinkedList()
    list.insert("c")
    list.insert("b")
    list.insert("a")
    actual = list.__str__()
    expected = "{ a } -> { b } -> { c } -> None"
    assert actual == expected
def test_append_end():
    list = LinkedList()
    list.append_item('rock')
    list.append_item('paper')
    list.append_item('scissors')
    actual = list.head.data
    expected = "rock"
    assert actual == expected
def test_kth_negative():
    link_list = LinkedList()
    link_list.insert(1)
    link_list.insert(2)
    link_list.insert(3)
    link_list.insert(4)
    with pytest.raises(Exception):
        link_list.kthFromEnd(-4)
def test_kth_equal_to_ll_length():
    link_list = LinkedList()
    link_list.insert(1)
    link_list.insert(2)
    link_list.insert(3)
    link_list.insert(4)
    with pytest.raises(Exception):
        link_list.kthFromEnd(4)
def test_kth_greater_than_ll_length():
    link_list = LinkedList()
    link_list.insert(1)
    link_list.insert(2)
    link_list.insert(3)
    link_list.insert(4)
    with pytest.raises(Exception):
        link_list.kthFromEnd(5)
def test_append_node_to_list():
    link_list = LinkedList()
    link_list.insert('b')
    link_list.insert('c')
    link_list.append('a')
    actual = str(link_list)
    expected = "{c}->{b}->{a}->NULL"
    assert actual == expected
    def add(self, key, value):
        hash_index = self.hash(key)

        if not self.buckets[hash_index]:
            self.buckets[hash_index] = LinkedList()

        bucket = self.buckets[hash_index]

        bucket.insert([key, value])
def test_kth_middle_value():
    link_list = LinkedList()
    link_list.insert(1)
    link_list.insert(10)
    link_list.insert(100)
    link_list.insert(1000)
    actual = link_list.kthFromEnd(2)
    expected = 100
    assert actual == expected
def test_add_after_value():
    list = LinkedList()
    list.insert('bumpers')
    list.insert('baby')
    list.insert('rubber')
    list.inject_a('baby', 'buggy')
    actual = list.find_all()
    expected = ['rubber', 'baby', 'buggy', 'bumpers']
    assert actual == expected
def test_x_fromend0():
    list = LinkedList()
    list.append_item('rock')
    list.append_item('paper')
    list.append_item('scissors')
    list.append_item('machinegun')
    actual = list.x_fromend(0)
    expected = 'machinegun'
    assert actual == expected
def test_not_includes():
    list = LinkedList()
    list.insert('rubber')
    list.insert('baby')
    list.insert('buggy')
    list.insert('bumpers')
    actual = list.includes('rad')
    expected = False
    assert actual == expected
def test_add_after_value1():
    list = LinkedList()
    list.append_item('rock')
    list.append_item('paper')
    list.append_item('scissors')
    list.append_item('machinegun')
    list.inject_a('scissors', 'cannon')
    actual = list.find_all()
    expected = ['rock', 'paper', 'scissors', 'cannon', 'machinegun']
    assert actual == expected
Ejemplo n.º 24
0
    def set(self, key, value):
        # this hashtable only supports strings as keys
        hashed_key_index = self._hash(key)

        # if we dont have a LinkedList yet at this bucket, instantiate one
        if not self._buckets[hashed_key_index]:
            self._buckets[hashed_key_index] = LinkedList()

        # insert both the key and the value as a tuple in the linked list
        self._buckets[hashed_key_index].insert((key, value))
def test_insert_after_last():
    link_list = LinkedList()
    link_list.insert('c')
    link_list.insert('d')
    link_list.append('b')
    link_list.append('a')
    link_list.insertAfter('a', 'yasss')
    actual = str(link_list)
    expected = "{d}->{c}->{b}->{a}->{yasss}->NULL"
    assert actual == expected
def test_insert_before_middle():
    link_list = LinkedList()
    link_list.insert('c')
    link_list.insert('d')
    link_list.append('b')
    link_list.append('a')
    link_list.insertBefore('b', 'yasss')
    actual = str(link_list)
    expected = "{d}->{c}->{yasss}->{b}->{a}->NULL"
    assert actual == expected
def test_insert_multiple_nodes():
    val1 = 'Trump'
    val2 = 'Melania'
    val3 = 'Conway'
    val4 = 'Huckabee'
    linked_list = LinkedList(val1)
    linked_list.insert(val2)
    linked_list.insert(val3)
    linked_list.insert(val4)
    actual = linked_list.head.value
    expected = 'Huckabee'
    assert actual == expected
def test_insert():
    list = LinkedList(Node("Buddy"))
    assert list.head.data == "Buddy"
    list.insert('Guy')
    list.insert('Friend')
    assert list.head.data == 'Friend'
def test_x_fromend_neg():
    list = LinkedList()
    list.append_item('machinegun')
    actual = list.x_fromend(-2)
    expected = 'Exception'
    assert actual == expected
def test_x_fromend_one_item():
    list = LinkedList()
    list.append_item('machinegun')
    actual = list.x_fromend(0)
    expected = 'machinegun'
    assert actual == expected