コード例 #1
0
def test_add_first_one_item():
    linked_list = LinkedList()
    linked_list.add_first(1)

    assert len(linked_list) == 1
    assert linked_list.head == 1
    assert linked_list.tail == 1
コード例 #2
0
ファイル: stack.py プロジェクト: eltonscm/learn-algorithms
class Stack:
    def __init__(self) -> None:
        self._list = LinkedList()

    def __len__(self) -> int:
        return len(self._list)

    def __iter__(self) -> Generator[T]:
        for item in self._list:
            yield item

    def push(self, data: T) -> None:
        self._list.add_first(data)

    def peek(self) -> T:
        if len(self._list) == 0:
            raise IndexError("peek from empty list")
        return self._list.head.data

    def pop(self) -> T:
        if len(self._list) == 0:
            raise IndexError("pop from empty list")
        data = self._list.head.data
        self._list.remove_first()
        return data
コード例 #3
0
ファイル: hash_table.py プロジェクト: AliEzzatOdeh/ADpy
    def add_entry(self, key, value):
        hash_for_item_to_add = self._compute_hash_code(key)
        index_for_item_to_add = self._compute_index_from_hash(
            hash_for_item_to_add)
        hash_table_entry = HashTableEntry(key, value)
        node_to_add = LinkedListNode(hash_table_entry)

        if self.data_array[index_for_item_to_add] == None:
            linked_list = LinkedList()
            linked_list.add_first(node_to_add)
            self.data_array[index_for_item_to_add] = linked_list
            self.current_items_count += 1
        else:  #check if same key exist and update it
            linked_list = self.data_array[index_for_item_to_add]
            node = linked_list.head
            while node != None:
                if (node.node_value.key == key):
                    node.node_value.value = value
                    break
                node = node.next
            if node == None:  #if key not exist then it is a new element just add it to linked list at that index
                linked_list.add_last(node_to_add)
                self.current_items_count += 1

        self._assure_max_size_Not_exceeded()
コード例 #4
0
ファイル: test_linked_list.py プロジェクト: AliEzzatOdeh/ADpy
    def test_delete_nodes(self):
        node1 = LinkedListNode("node 1")
        node2 = LinkedListNode("node 2")
        node3 = LinkedListNode("node 3")
        node4 = LinkedListNode("node 4")
        my_linked_list = LinkedList()
        my_linked_list.add_first(node1)
        my_linked_list.add_last(node2)
        my_linked_list.add_last(node3)
        my_linked_list.add_last(node4)

        my_linked_list.delete_node(node1)
        self.assertEqual(my_linked_list.length, 3)
        self.assertEqual(my_linked_list.head, node2)
        my_linked_list.delete_node(node3)
        self.assertEqual(my_linked_list.length, 2)
        self.assertEqual(my_linked_list.get_node_by_index(1), node4)
        my_linked_list.delete_node(node4)
        self.assertEqual(my_linked_list.length, 1)
        self.assertEqual(my_linked_list.head, node2)
        self.assertEqual(my_linked_list.tail, node2)
        with self.assertRaises(ValueError):
            my_linked_list.delete_node(node4)
        my_linked_list.delete_node(node2)
        self.assertEqual(my_linked_list.length, 0)
        self.assertEqual(my_linked_list.head, None)
        self.assertEqual(my_linked_list.tail, None)
コード例 #5
0
def test_add_first_two_items():
    linked_list = LinkedList()
    linked_list.add_first(1)
    linked_list.add_first(2)

    assert len(linked_list) == 2
    assert linked_list.head == 2
    assert linked_list.tail == 1
    assert linked_list.tail.next_ is None
コード例 #6
0
def test_remove_last_with_one_item():
    linked_list = LinkedList()
    linked_list.add_first(1)

    linked_list.remove_last()

    assert len(linked_list) == 0
    assert linked_list.head is None
    assert linked_list.tail is None
コード例 #7
0
def test_remove_last_with_two_items():
    linked_list = LinkedList()
    linked_list.add_first(1)
    linked_list.add_first(2)

    linked_list.remove_last()

    assert len(linked_list) == 1
    assert linked_list.head == 2
    assert linked_list.tail == 2
コード例 #8
0
ファイル: test_linked_list.py プロジェクト: AliEzzatOdeh/ADpy
 def test_add_first_to_empty_list(self):
     node1 = LinkedListNode("node 1")
     my_linked_list = LinkedList()
     my_linked_list.add_first(node1)
     print(my_linked_list)
     self.assertEqual(my_linked_list.head.node_value, 'node 1')
     self.assertEqual(my_linked_list.head.next, None)
     self.assertEqual(my_linked_list.tail.node_value, 'node 1')
     self.assertEqual(my_linked_list.tail.next, None)
     self.assertEqual(my_linked_list.length, 1)
コード例 #9
0
ファイル: test_linked_list.py プロジェクト: AliEzzatOdeh/ADpy
 def test_iteration_of_nodes(self):
     node1 = LinkedListNode("node 1")
     node2 = LinkedListNode("node 2")
     node3 = LinkedListNode("node 3")
     my_linked_list = LinkedList()
     my_linked_list.add_first(node1)
     my_linked_list.add_last(node2)
     my_linked_list.add_last(node3)
     counter = 0
     for node in my_linked_list:
         self.assertEqual(node, my_linked_list.get_node_by_index(counter))
         counter += 1
コード例 #10
0
ファイル: test_linked_list.py プロジェクト: AliEzzatOdeh/ADpy
 def test_get_node_by_index(self):
     node1 = LinkedListNode("node 1")
     node2 = LinkedListNode("node 2")
     my_linked_list = LinkedList()
     my_linked_list.add_first(node1)
     my_linked_list.add_last(node2)
     print(my_linked_list)
     with self.assertRaises(IndexError):
         my_linked_list.get_node_by_index(2)
     self.assertEqual(
         my_linked_list.get_node_by_index(0).node_value, 'node 1')
     self.assertEqual(
         my_linked_list.get_node_by_index(1).node_value, 'node 2')
コード例 #11
0
ファイル: test_linked_list.py プロジェクト: AliEzzatOdeh/ADpy
    def test_find_node_by_value(self):
        node1 = LinkedListNode("node 1")
        node2 = LinkedListNode("node 2")
        node3 = LinkedListNode("node 3")
        my_linked_list = LinkedList()
        my_linked_list.add_first(node1)
        my_linked_list.add_node_after(node1, node2)
        my_linked_list.add_last(node3)

        node_to_find = my_linked_list.find_node_by_value("node 1")
        self.assertEqual(node_to_find, node1)
        node_to_find = my_linked_list.find_node_by_value("node 2")
        self.assertEqual(node_to_find, node2)
        node_to_find = my_linked_list.find_node_by_value("node 3")
        self.assertEqual(node_to_find, node3)
コード例 #12
0
ファイル: test_linked_list.py プロジェクト: AliEzzatOdeh/ADpy
 def test_add_node_after(self):
     node1 = LinkedListNode("node 1")
     node2 = LinkedListNode("node 2")
     node3 = LinkedListNode("node 3")
     node4 = LinkedListNode("node 4")
     my_linked_list = LinkedList()
     my_linked_list.add_first(node1)
     my_linked_list.add_node_after(node1, node2)
     self.assertEqual(my_linked_list.head.node_value, 'node 1')
     self.assertEqual(
         my_linked_list.get_node_by_index(1).node_value, 'node 2')
     self.assertEqual(my_linked_list.length, 2)
     with self.assertRaises(ValueError):
         my_linked_list.add_node_after(node3, node4)
     my_linked_list.add_node_after(node2, node3)
     self.assertEqual(
         my_linked_list.get_node_by_index(1).node_value, 'node 2')
     self.assertEqual(
         my_linked_list.get_node_by_index(2).node_value, 'node 3')
     self.assertEqual(my_linked_list.tail.node_value, 'node 3')
     self.assertEqual(my_linked_list.length, 3)
     print(my_linked_list)
コード例 #13
0
class TestLinkedList(TestCase):
    faker = Faker()
    list: LinkedList
    num_nodes = 5

    def setUp(self):
        nodes = [Node(x) for x in range(0, self.num_nodes)]
        self.list = LinkedList()
        for node in nodes:
            self.list.add(node)

    def test_add_node(self):
        random_node = Node(self.faker.sentence())
        self.list.add(random_node)

        last_node = self.list.find_last()

        self.assertEqual(
            last_node,
            random_node,
            f'Last node: {last_node} does not equal {random_node}.'
        )

    def test_add_first_node(self):
        random_node = Node(self.faker.sentence())
        self.list.add_first(random_node)

        self.assertEqual(
            self.list.head,
            random_node,
            f'Head node of list: {self.list.head} does not equal {random_node}'
        )

    def test_traverse(self):
        counter = 0
        for node in self.list.traverse():
            self.assertEqual(
                node.data,
                counter,
                f'Node data {node.data} does not equal {counter}.'
            )
            counter += 1

    def test_add_after(self):
        random_index = random.randint(0, self.num_nodes - 1)
        node = self.list.find(Node(random_index))

        random_node = Node(random.randint(self.num_nodes + 1, 100))
        self.list.add_after(node, random_node)

        self.assertEqual(
            node.next,
            random_node,
            f'Next node of {node} does not equal {random_node}'
        )

    def test_add_before(self):
        random_index = random.randint(0, self.num_nodes - 1)
        node = self.list.find(Node(random_index))

        random_node = Node(random.randint(self.num_nodes + 1, 100))
        self.list.add_before(node, random_node)

        self.assertEqual(
            random_node.next.data,
            node.data,
            f'Next node of {random_node}: {random_node.next} '
            f'does not equal {node}'
        )

    def test_find(self):
        random_index = random.randint(0, self.num_nodes - 1)
        random_node_to_find = Node(random_index)

        found_node = self.list.find(random_node_to_find)

        self.assertEqual(
            random_node_to_find.data,
            found_node.data,
            f'Found Node {found_node.data} is not equal '
            f'to {random_node_to_find.data}'
        )

    def test_find_non_existent(self):
        random_non_existent_index = random.randint(self.num_nodes + 1, 1000)
        res_should_be_none = self.list.find(Node(random_non_existent_index))

        self.assertIsNone(
            res_should_be_none,
            f'Result: {res_should_be_none} is not None as expected.'
        )

    def test_find_last(self):
        last_node = Node(self.num_nodes - 1)
        found_node = self.list.find_last()

        self.assertEqual(
            last_node.data,
            found_node.data,
            f'Last node {found_node} not equal to expected {last_node}'
        )

    def test_find_before(self):
        random_index_not_head = random.randint(1, self.num_nodes - 1)
        random_node = Node(random_index_not_head)

        node_before = self.list.find_before(random_node)

        self.assertEqual(
            node_before.data,
            random_index_not_head - 1,
            f'Node before {node_before.data} is not equal '
            f'to {random_index_not_head - 1}'
        )

    def test_delete(self):
        random_node = Node(random.randint(0, self.num_nodes - 1))
        self.list.delete(random_node)

        should_be_none = self.list.find(random_node)

        self.assertIsNone(
            should_be_none,
            f'{should_be_none} is not None as expected.'
        )