Exemplo n.º 1
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.º 2
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.º 3
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)
Exemplo n.º 4
0
class UnsafeHashTable:
    def __init__(self):
        self.first_bucket = LinkedList()
        self.second_bucket = LinkedList()
        self.third_bucket = LinkedList()
        self.Sum = 0
        self.NodeCount = 0

    def add(self, key, value):
        node = DecoratedNode(key, value)

        bucket_number = node.key % 3
        if bucket_number == 1:
            self.first_bucket.add_node(node)
            self.NodeCount += 1
            return
        if bucket_number == 2:
            self.NodeCount += 1
            self.second_bucket.add_node(node)
            return
        if bucket_number == 0:
            self.NodeCount += 1
            self.third_bucket.add_node(node)
            return
        raise ValueError(
            "[ ERROR ] Adding Node to the HashTable failed - impossible index: "
            + str(bucket_number))

    def get(self, key: int):
        bucket_number = key % 3
        if bucket_number == 1:
            result = self.first_bucket.find_node(key)
            return result
        if bucket_number == 2:
            result = self.second_bucket.find_node(key)
            return result
        if bucket_number == 0:
            result = self.third_bucket.find_node(key)
            return result