Esempio n. 1
0
class UnionTest(unittest.TestCase):
    def setUp(self):
        self.__dll_1 = DoubleLinkedList()
        self.__dll_2 = DoubleLinkedList()
        for element in range(0, 4):
            self.__dll_1.top(element)
        for element in range(4, 7):
            self.__dll_2.top(element)

    def test_union(self):
        u_dll = union(self.__dll_1, self.__dll_2)
        self.assertEqual(u_dll.to_list(), [6, 5, 4, 3, 2, 1, 0])

        # Print union linked list [6, 5, 4, 3, 2, 1, 0]
        print(u_dll.to_list())

    def test_union_empty_ll(self):
        dll_1 = DoubleLinkedList()
        dll_2 = DoubleLinkedList()
        u_dll = union(dll_1, dll_2)

        # Empty linked list. returns empty list
        self.assertEqual(u_dll.to_list(), [])
        print(u_dll.to_list())

        # Linked lists are None
        self.assertIsNone(union(None, None))
        print(union(None, None))
Esempio n. 2
0
 def setUp(self):
     self.__dll_1 = DoubleLinkedList()
     self.__dll_2 = DoubleLinkedList()
     for element in range(0, 4):
         self.__dll_1.top(element)
     for element in range(4, 7):
         self.__dll_2.top(element)
Esempio n. 3
0
class Blockchain:
    def __init__(self):
        self.__dll = DoubleLinkedList()
        self.__last_block = None

    def is_empty(self):
        return self.__dll.size() == 0

    def add_element(self, data):
        # Data is None
        if data is None or len(data) == 0:
            return False
        # linked list is empty
        if self.__dll.head is None:
            new_block = Block(data, 0)
            self.__last_block = new_block
            self.__dll.append(new_block)
            return True
        # linked list is not empty
        else:
            new_block = Block(data, self.__last_block.hash)
            self.__last_block = new_block
            self.__dll.append(new_block)
            return True

    def __iter__(self):
        for item in self.__dll:
            yield item

    def __repr__(self):
        return "\n".join(str(x) for x in self.__iter__())
Esempio n. 4
0
 def setUp(self):
     self.__dll_1 = DoubleLinkedList()
     self.__dll_2 = DoubleLinkedList()
     for element in range(10, 16):
         self.__dll_1.append(element)
     for element in range(10, 13):
         self.__dll_2.append(element)
Esempio n. 5
0
    def test_union_empty_ll(self):
        dll_1 = DoubleLinkedList()
        dll_2 = DoubleLinkedList()
        u_dll = union(dll_1, dll_2)

        # Empty linked list. returns empty list
        self.assertEqual(u_dll.to_list(), [])
        print(u_dll.to_list())

        # Linked lists are None
        self.assertIsNone(union(None, None))
        print(union(None, None))
Esempio n. 6
0
def intersection(dll_1, dll_2):
    if dll_1 is None or dll_2 is None or dll_1.size() == 0 or dll_2.size(
    ) == 0:
        return None

    inter_dll = DoubleLinkedList()
    copy_dll_1 = DoubleLinkedList()
    for element in dll_1:
        copy_dll_1.append(element)

    for element in dll_2:
        if element in copy_dll_1:
            inter_dll.append(element)
            copy_dll_1.remove_element(element)
    return inter_dll
Esempio n. 7
0
    def test_empty_lists(self):
        dll_1 = DoubleLinkedList()
        dll_2 = DoubleLinkedList()

        # Empty lists, there is no intersection. Returns None
        self.assertIsNone(intersection(dll_1, dll_2))
        print(intersection(dll_1, dll_2))

        # None input, returns None
        self.assertIsNone(intersection(None, None))
        print(intersection(None, None))

        # If one list is empty there is no intersection. Returns None
        self.assertIsNone(intersection(dll_1, self.__dll_2))
        self.assertIsNone(intersection(self.__dll_1, dll_2))
        print(intersection(dll_1, self.__dll_2))
        print(intersection(self.__dll_1, dll_2))

        # if one list is None there is no intersection. Returns None
        self.assertIsNone(intersection(None, self.__dll_2))
        self.assertIsNone(intersection(self.__dll_1, None))
        print(intersection(None, self.__dll_2))
        print(intersection(self.__dll_1, None))
Esempio n. 8
0
class IntersectionTest(unittest.TestCase):
    def setUp(self):
        self.__dll_1 = DoubleLinkedList()
        self.__dll_2 = DoubleLinkedList()
        for element in range(10, 16):
            self.__dll_1.append(element)
        for element in range(10, 13):
            self.__dll_2.append(element)

    def test_intersection(self):
        i_dll = intersection(self.__dll_1, self.__dll_2)

        # Returns only the intersection of numbers within the two lists
        self.assertEqual(i_dll.to_list(), [10, 11, 12])
        # Print [10, 11, 12]
        print(i_dll.to_list())

        # Adding elements doesn't affect it
        self.__dll_2.append(30)
        self.__dll_1.append(33)
        self.assertEqual(i_dll.to_list(), [10, 11, 12])
        print(i_dll.to_list())

    def test_empty_lists(self):
        dll_1 = DoubleLinkedList()
        dll_2 = DoubleLinkedList()

        # Empty lists, there is no intersection. Returns None
        self.assertIsNone(intersection(dll_1, dll_2))
        print(intersection(dll_1, dll_2))

        # None input, returns None
        self.assertIsNone(intersection(None, None))
        print(intersection(None, None))

        # If one list is empty there is no intersection. Returns None
        self.assertIsNone(intersection(dll_1, self.__dll_2))
        self.assertIsNone(intersection(self.__dll_1, dll_2))
        print(intersection(dll_1, self.__dll_2))
        print(intersection(self.__dll_1, dll_2))

        # if one list is None there is no intersection. Returns None
        self.assertIsNone(intersection(None, self.__dll_2))
        self.assertIsNone(intersection(self.__dll_1, None))
        print(intersection(None, self.__dll_2))
        print(intersection(self.__dll_1, None))
Esempio n. 9
0
def union(dll_1, dll_2):
    if dll_1 is None and dll_2 is None:
        return None
    if dll_1.size == 0:
        return dll_2
    elif dll_2.size == 0:
        return dll_1
    elif dll_1.size == 0 and dll_2.size == 0:
        return list()

    union_dll = DoubleLinkedList()
    for element in dll_2:
        union_dll.append(element)
    for element in dll_1:
        union_dll.append(element)
    return union_dll
Esempio n. 10
0
 def __init__(self, initial_size=10):
     self.bucket = [DoubleLinkedList() for _ in range(initial_size)]
     self.prime = 123
     self.num_entries = 0
Esempio n. 11
0
 def __init__(self):
     self.__dll = DoubleLinkedList()
     self.__last_block = None
Esempio n. 12
0
 def setUp(self):
     self.__value = 2
     self.__node = Node(1)
     self.__dll = DoubleLinkedList()
Esempio n. 13
0
class DoubleLinkedListTest(unittest.TestCase):
    def setUp(self):
        self.__value = 2
        self.__node = Node(1)
        self.__dll = DoubleLinkedList()

    def test_append_dll(self):
        # Empty list
        self.assertTrue(self.__dll.append(self.__value))
        # Not empty list
        self.assertTrue(self.__dll.append(self.__value))

    def test_size_dll(self):
        self.__dll.append(self.__value)
        self.assertEqual(self.__dll.size(), 1)

    def test_top_dll(self):
        self.__dll.append(self.__value)
        self.assertTrue(self.__dll.top(1))

    def test_top_node_dll(self):
        self.__dll.head = self.__node
        self.__dll.tail = self.__node
        self.__dll.top_node(Node(0))
        self.assertEqual(self.__dll.head.value, 0)
        self.assertEqual(self.__dll.tail.value, 1)

        self.__dll.top_node(Node(5))
        self.assertEqual(self.__dll.head.value, 5)
        self.assertEqual(self.__dll.tail.value, 1)

    def test_remove_element(self):
        self.__dll.append(5)
        self.__dll.append(6)

        # Remove head
        self.assertTrue(self.__dll.remove_element(5))

        # Remove tail
        self.assertTrue(self.__dll.remove_element(6))

        # Remove middle value
        for element in range(5, 8):
            self.__dll.append(element)
        self.assertTrue(self.__dll.remove_element(6))

    def test_remove_node(self):
        self.__dll.top_node(self.__node)
        self.__dll.top_node(Node(self.__value))

        # Remove head
        self.assertTrue(self.__dll.remove_node(self.__node))

        # Remove tail
        self.__dll.remove_element(2)
        self.__dll.top_node(self.__node)
        self.__dll.append(self.__value)
        self.assertTrue(self.__dll.remove_node(self.__node))

        # Remove middle
        self.__dll.remove_element(2)
        self.__dll.append(1)
        self.__dll.top_node(self.__node)
        self.__dll.top(0)
        self.assertTrue(self.__dll.remove_node(self.__node))

    def test_pop_tail(self):
        for element in range(0, 2):
            self.__dll.append(element)
        self.assertTrue(self.__dll.pop_tail())
        self.__dll.remove_element(0)
        # No elements to removed. Empty dll
        self.assertFalse(self.__dll.pop_tail(), False)

    def test_search(self):
        self.__dll.append(self.__value)
        # Search existing element
        self.assertTrue(self.__dll.search(2))
        # Search non existing element
        self.assertFalse(self.__dll.search(9))
        # Search null in list
        self.assertFalse(self.__dll.search(None))

    def test_to_list(self):
        for element in range(0, 3):
            self.__dll.append(element)
        self.assertEqual(self.__dll.to_list(), [0, 1, 2])