def test_full_linked_list_deep_equality():
    ll1 = LinkedList()
    ll2 = LinkedList()
    ll1.append(0)
    ll1.insert(1)
    ll2.insert(0)
    assert ll1.head.next_.val == ll2.head.val
def test_ll_merge_uneven_long_second():
    ll1 = LinkedList()
    ll1.append(2)
    ll1.append(4)
    ll1.append(6)
    ll2 = LinkedList()
    ll2.append(1)
    mergedLists = LinkedList.merge_lls(ll2, ll1)
    assert str(mergedLists) == "{ 1 } -> { 2 } -> { 4 } -> { 6 } -> NULL"
    assert mergedLists.length == 4
def test_ll_merge_uneven_long_first():
    ll1 = LinkedList()
    ll1.append(2)
    ll2 = LinkedList()
    ll2.append(1)
    ll2.append(3)
    ll2.append(5)
    mergedLists = LinkedList.merge_lls(ll2, ll1)
    assert str(mergedLists) == "{ 1 } -> { 2 } -> { 3 } -> { 5 } -> NULL"
    assert mergedLists.length == 4
def test_ll_merge1():
    ll1 = LinkedList()
    ll1.append(2)
    ll1.append(4)
    ll1.append(6)
    ll2 = LinkedList()
    ll2.append(1)
    ll2.append(3)
    ll2.append(5)
    mergedLists = LinkedList.merge_lls(ll2, ll1)
    assert (str(mergedLists) ==
            "{ 1 } -> { 2 } -> { 3 } -> { 4 } -> { 5 } -> { 6 } -> NULL")
    assert mergedLists.length == 6
def test_ll_append2():
    linkedlist = LinkedList()
    linkedlist.insert(0)
    linkedlist.append(1)
    linkedlist.insert(2)
    linkedlist.append(3)
    assert str(linkedlist) == "{ 2 } -> { 0 } -> { 1 } -> { 3 } -> NULL"
def test_insert_after2():
    ll = LinkedList()
    ll.insert(0)
    ll.insert(1)
    ll.insert(2)
    ll.insert_after(0, 5)
    assert str(ll) == "{ 2 } -> { 1 } -> { 0 } -> { 5 } -> NULL"
def test_ll_insert_before5():
    ll = LinkedList()
    ll.append(5)
    ll.append(3)
    ll.append(1)
    with pytest.raises(Exception):
        ll.insert_before(7, 10)
def test_ll_insert_before4():
    ll = LinkedList()
    ll.append(5)
    ll.append(3)
    ll.append(1)
    ll.insert_before(5, 10)
    assert str(ll) == "{ 10 } -> { 5 } -> { 3 } -> { 1 } -> NULL"
Exemplo n.º 9
0
def handle_linked_random(size):
    linked_list = LinkedList()
    array = fill_random(size)
    times = measure_time(array, linked_list)
    print('LL, append, {} size, random, {} s'.format(
        size, round(times['append'] / size, 5)))
    print('LL, find, {} size, random, {} s'.format(
        size, round(times['find'] / size, 5)))
def test_kth_from_end2s():
    ll = LinkedList()
    ll.insert(0)
    ll.insert(1)
    ll.insert(2)
    ll.insert(3)
    ll.insert(4)
    ll.insert(5)
    assert ll.kth_from_end(3) == 2
Exemplo n.º 11
0
    def test_add_to_list(self):
        name = "Dust"
        matric = "1234"
        year = 3

        node = Node(matric, name, year)
        linked_list = LinkedList()
        linked_list.add_to_list(node)

        self.assertEqual(linked_list.get_root(), node)
Exemplo n.º 12
0
    def test_find_in_list(self):
        names = ("James", "1234", 10), ("Kobe", "3456", 20), ("Curry", "7890",
                                                              8)

        nodes = [Node(matric, name, year) for name, matric, year in names]

        linked_list = LinkedList()
        for node in nodes:
            linked_list.add_to_list(node)

        marker = linked_list.get_root()
        for i in range(len(nodes) - 1, -1, -1):
            self.assertEqual(nodes[i], linked_list.find(marker.name))
            marker = marker.get_next()
Exemplo n.º 13
0
    def test_PutElements_ExpectCorrectCount(self):

        # arrange
        batch_size = 10
        linked_list = LinkedList()

        # act
        for i in range(1, batch_size + 1):
            new_node = DecoratedNode(i, i * 10)
            linked_list.add_node(new_node)

        print(linked_list)

        # assert
        self.assertEqual(batch_size, linked_list.NodeCount)
Exemplo n.º 14
0
def obtain_list():
    """
    Creates linked list of a given batch size.
    :return: Returns a linked list which contains of key 1 to batch_size and values which are 1
    """
    timer_start = datetime.now()

    linked_list = LinkedList()
    for i in range(1, batch_size + 1):
        new_node = DecoratedNode(i, 1)
        linked_list.add_node(new_node)

    timer_end = datetime.now()
    print("[ LL TIME ] Linked list creation finished in " +
          str(timer_end - timer_start))
    return linked_list
Exemplo n.º 15
0
    def test_PutElements_ExpectThemToBePresent(self):
        # arrange
        batch_size = 10
        linked_list = LinkedList()
        original_descriptions = []

        # act
        for i in range(1, batch_size + 1):
            new_node = DecoratedNode(i, i * 10)
            original_descriptions.append(new_node.__str__())
            linked_list.add_node(new_node)

        resulting_descriptions = linked_list.__str__().split(', ')

        # assert
        for resulting_description in resulting_descriptions:
            self.assertIn(resulting_description, original_descriptions)
def test_ll_append1():
    linkedlist = LinkedList()
    linkedlist.insert(0)
    linkedlist.append(1)
    assert str(linkedlist) == "{ 0 } -> { 1 } -> NULL"
def test_ll_merge_one_empty_list():
    ll1 = LinkedList()
    ll1.append(0)
    ll2 = LinkedList()
    mergedLists = LinkedList.merge_lls(ll2, ll1)
    assert str(mergedLists) == "{ 0 } -> NULL"
Exemplo n.º 18
0
    def test_find_missing_in_list(self):
        linked_list = LinkedList()

        with self.assertRaises(LookupError):
            linked_list.find("Dust")
def test_LinkedList_takes_node():
    node1 = Node(1)
    ll1 = LinkedList()
    assert 1 == 1
def test_insert_into_ll2():
    linklist = LinkedList()
    linklist.insert(1)
    linklist.insert(5)
    assert linklist.head.val == 5
Exemplo n.º 21
0
    def test_list_creation(self):
        linked_list = LinkedList()

        self.assertIsNone(linked_list.get_root())
Exemplo n.º 22
0
 def __init__(self):
     self.first_bucket = LinkedList()
     self.second_bucket = LinkedList()
     self.third_bucket = LinkedList()
     self.Sum = 0
     self.NodeCount = 0
Exemplo n.º 23
0
from linked_list.node import Node
from linked_list.LinkedList import LinkedList

nba_menber = LinkedList()

nba_menber.add_to_list(Node('aa', 'Kobe', 40))
nba_menber.add_to_list(Node('aa', 'James', 35))
nba_menber.add_to_list(Node('aa', 'Curry', 31))
nba_menber.add_to_list(Node('aa', 'Durant', 30))
nba_menber.add_to_list(Node('aa', 'Kwaii', 28))

# nba_menber.print_list()
nba_menber.find("James").print_details()
            next_reversed_half = reversed_half.next
            temp = h.next
            h.next = reversed_half
            reversed_half.next = temp
            reversed_half = next_reversed_half
            h = temp


if __name__ == "__main__":
    ###################################################################################################################
    ###################################################################################################################
    ############################################ REVERSE SINGLY LINKED LIST 206 #######################################
    ###################################################################################################################
    ###################################################################################################################
    s = Solution()
    ll = LinkedList([1, 2, 3, 4, 5])
    head = s.reverseList(ll.root)
    ll.print_list(head)

    ###################################################################################################################
    ###################################################################################################################
    ############################################ HAS CYCLE 141 ########################################################
    ###################################################################################################################
    ###################################################################################################################

    ll = LinkedList([1])
    print(s.hasCycle(ll.root))

    ###################################################################################################################
    ###################################################################################################################
    ############################################ MERGE TWO SORTED LISTS 21 ############################################
def test_includes_ll():
    linklist = LinkedList()
    assert linklist.includes(5) == False
Exemplo n.º 26
0
 def __init__(self):
     self.__linked_list = LinkedList()
def test_ll_append3():
    linkedlist = LinkedList()
    linkedlist.append(1)
    assert linkedlist.head.val == 1
def test_includes2_ll():
    linklist = LinkedList()
    linklist.insert(0)
    assert linklist.includes(0) == True
def test_ll_string2():
    linklist = LinkedList()
    linklist.insert(0)
    linklist.insert(1)
    llstring = str(linklist)
    assert llstring == "{ 1 } -> { 0 } -> NULL"
def test_ll_repr2():
    linklist = LinkedList()
    linklist.insert(0)
    linklist.insert(1)
    llstring = repr(linklist)
    assert llstring == "[1, 0]"