예제 #1
0
def fixture_duplicates_linked_list():
    """
    Mocks list of duplicates in hashtable node
    """
    my_list = LinkedList()
    my_list.append(52)
    my_list.append(40)
    return my_list
def fixture_linked_list():
    """
    Mock list with some data
    """
    my_list = LinkedList()

    for i in range(5):
        my_list.append(i)

    return my_list
예제 #3
0
class Queue:
    """Basic queue implementation"""

    def __init__(self):
        self._list = LinkedList()

    def enqueue(self, value) -> None:
        """
        Add item to the queue to end of the queue

        :param value: Value to be added
        :return: None
        """
        return self._list.append(value)

    def dequeue(self) -> Any:
        """
        Return first element of the queue if present

        :return: First element of the queue
        :rtype: Any
        """
        return self._list.rpop()
예제 #4
0
class Stack:
    """Basic stack implementation"""
    def __init__(self):
        self._list = LinkedList()

    def push(self, value: Any):
        """
        Push given element to the stack

        :param value: Value to add
        :return: None
        """
        self._list.append(value)

    def pop(self) -> Any:
        """
        Pop top element from the stack

        :return: Value from the top
        :rtype: Any
        """
        return self._list.pop()

    def peek(self) -> Any:
        """
        Takes value from the top without deleting

        :return: Value from the top, None if empty
        :rtype: Any
        """
        if self._list.tail is None:
            return None
        return self._list.tail.value

    def __iter__(self):
        return iter(self._list)
def test_linked_list_counter(test_list, expected):
    empty_linked_list = LinkedList()
    for item in test_list:
        empty_linked_list.append(item)

    if expected != 0:
        assert empty_linked_list.counter == expected + 2
    empty_linked_list.pop()
    empty_linked_list.pop()
    assert empty_linked_list.counter == expected
def fixture_empty_linked_list():
    """
    Mock empty linked list
    """
    return LinkedList()
예제 #7
0
 def __init__(self):
     self._list = LinkedList()
예제 #8
0
def test_hashtable_delete(filled_hashtable, test_keys):
    for test_key in test_keys:
        filled_hashtable.delete(test_key)
        assert filled_hashtable.lookup(test_key).value == LinkedList()
예제 #9
0
class HashTable:
    """
    Contains hashtable implementation
    """
    LIST_LENGTH = 15

    def __init__(self):
        self._list = LinkedList()

        for _ in range(self.LIST_LENGTH):
            self._list.append(LinkedList())

    @staticmethod
    def _hash_key(key) -> int:
        """
        Hash key with simple 'str->int code' function

        :param key: Value to be hashed
        :return: Integer that correspond to the key
        :rtype: int
        """
        hash_code = 0

        for index, item in enumerate(str.encode(key, "utf-8")):
            hash_code += (item * (index + 1))
        return hash_code % HashTable.LIST_LENGTH

    def insert(self, key, value):
        """
        Inserts pair of (key, value) to the table

        :param key: Key to be searched for
        :param value: Data to stored
        :return: None
        """
        if not isinstance(key, str):
            raise TypeError

        index = self._hash_key(key)

        self._list[index].value.append(value)

    def lookup(self, key):
        """
        Return data stored with the given key

        :param key: Key to look up
        :return: List of elements that stored in this cell
        """
        index = self._hash_key(key)

        return self._list[index]

    def delete(self, key, value=None):
        """
        Delete elements stored with th given key

        :param key: Key of element to delete
        :param value: If there is multiple elements delete all of them if value=
        None, specific element otherwise
        :return: None
        """

        index = self._hash_key(key)

        if value is None:
            self._list.delete(index)

        value_index = self._list[index].value.lookup(value)

        self._list[index].value.delete(value_index)
예제 #10
0
    def __init__(self):
        self._list = LinkedList()

        for _ in range(self.LIST_LENGTH):
            self._list.append(LinkedList())