Ejemplo n.º 1
0
 def test_7(self):
     """ next of prior head is now head """
     dl = DoubleLinkedList()
     n1 = Node(5)
     n2 = Node(3)
     dl.push(n1)
     dl.push(n2)
     dl.pop()
     self.assertEqual(dl.head._value, 5)
Ejemplo n.º 2
0
	def test_6(self):
		"""check that pop removes head"""
		n1 = Node('A')
		n2 = Node('B')
		dl = DoubleLinkedList()
		dl.append(n1) #head and tail at this point
		dl.append(n2) # A is head and B is now tail
		dl.pop() # removes A so head value should be B
		self.assertEqual(dl.head._value, 'B') 
Ejemplo n.º 3
0
def find_components(g: Graph):
    """
    Breadth First Search Alg. Which also:
    tests if the graph is connected,
    computes the distance from s to the other edges
    labels the vertices in the order they are visited
    :param g: Graph
    :return: (isConnected, {dict of components})
    """
    visited = set()
    components = dict()
    count = 1

    while len(visited) < len(g.vertices):
        for o in g.vertices:
            if o not in visited:
                v = o
                break
        queue = DoubleLinkedList()  # queue
        queue.append(v)
        visited.add(v)
        components[count] = [v]

        while len(queue) > 0:
            w = queue.pop()  # BFS so FIFO
            for n in w.neighbours:
                # If w not visited
                if n not in visited:
                    visited.add(n)
                    queue.append(n)
                    components[count].append(n)
        count += 1

    is_connected = len(components) == 1
    return is_connected, components
Ejemplo n.º 4
0
class Deque(object):
    """Class deque for deque composition."""

    def __init__(self):
        """Init for deque."""
        self._new_dll = DoubleLinkedList()

    def append(self, val):
        """Enqueue function for queue pushes value to head."""
        return self._new_dll.push(val)

    def appendleft(self, val):
        """Append vals to the front of deque."""
        return self._new_dll.append(val)

    def pop(self):
        """Pop from dll."""
        return self._new_dll.pop()

    def popleft(self):
        """Shift for popleft."""
        return self._new_dll.shift()

    def peek(self):  # pragma no cover
        """Return a new value without dequeuing it."""
        print(self._new_dll.head.data)

    def peekleft(self):  # pragma no cover
        """Return a new value from the left without dequeing it."""
        print(self._new_dll.tail.data)

    def __len__(self):
        """Length function for the queue."""
        return len(self._new_dll)
Ejemplo n.º 5
0
class Queue(object):
    """Will create a instance of Queue and allow enqueue(), dequeue(), peek(), size() and len() methods to work."""
    def __init__(self, iterable=None):
        """Will init a new instance of the Stack class."""
        from dll import DoubleLinkedList
        self._dll = DoubleLinkedList()
        self._counter = 0

    def enqueue(self, data):
        """Will push a new element into the Queue."""
        self._dll.append(data)
        self._counter += 1

    def dequeue(self):
        """Will remove the first element in the Queue."""
        if self._dll.head:
            self._counter -= 1
        return self._dll.pop()

    def peek(self):
        """Will return the next value in the Queue."""
        return self._dll.head.data

    def __len__(self):
        """Will return the length of the stack using len()."""
        return self._counter
Ejemplo n.º 6
0
class Deque(object):
    """Will create a instance of Deque."""
    def __init__(self, iterable=None):
        """Will init a new instance of the Stack class."""
        from dll import DoubleLinkedList
        self._dll = DoubleLinkedList()
        self._counter = 0

    def append(self, data):
        """Will push a new element into the Deque."""
        self._dll.append(data)
        self._counter += 1

    def pop_left(self):
        """Will remove the head value."""
        if self._dll.head:
            self._counter -= 1
        else:
            raise ValueError('no value left to pop')
        return self._dll.pop()

    def append_left(self, data):
        """Append a value to the head."""
        self._dll.push(data)
        self._counter += 1

    def pop(self):
        """Pop a value from the end."""
        if self._dll.tail:
            self._counter -= 1
        else:
            raise ValueError('no value left to pop')
        return self._dll.shift()

    def peek_left(self):
        """Will return the value that pop_left would return."""
        if self._dll.head:
            return self._dll.head.data
        else:
            raise ValueError('no value in the deque to peek')

    def peek(self):
        """Will return the value that pop would return."""
        if self._dll.head:
            return self._dll.tail.data
        else:
            raise ValueError('no value in the deof correct typeque to peek')

    def size(self):
        """Return the size of the deque."""
        return self._counter

    def __len__(self):
        """Will return the length of the deque using len()."""
        return self._counter
Ejemplo n.º 7
0
class Deque(object):
    """Deque data structure, can add and remove from both ends."""
    def __init__(self):
        """Initialize the deque."""
        self.deque = DoubleLinkedList()

    def append(self, value):
        """Add an item to the back of the deque."""
        self.deque.append(value)

    def append_left(self, value):
        """Add an item to the front of the deque."""
        self.deque.push(value)

    def pop(self):
        """Pop a value off of the back of the deque and return it."""
        return self.deque.shift()

    def pop_left(self):
        """Pop a value off of the front of the deque and return it."""
        return self.deque.pop()

    def peek(self):
        """Return the next value that would be poped at the end of deque."""
        try:
            return self.deque.tail.data
        except AttributeError:
            return None

    def peek_left(self):
        """Return the next value to pop in the deque."""
        try:
            return self.deque.head.data
        except AttributeError:
            return None

    def size(self):
        """Return the number of nodes in the deque."""
        return self.deque.__len__()
Ejemplo n.º 8
0
class DoublyLinkedListCase(unittest.TestCase):
    dll = None

    def setUp(self):
        self.dll = DoubleLinkedList()

    def test_init(self):
        self.assertIsNone(self.dll._head, "Initial HEAD should be None")
        self.assertIsNone(self.dll._tail, "Initial TAIL should be None")
        self.assertEqual(0, len(self.dll), "Initial length should be zero")

    def test_append_left(self):
        self.dll.append_left(1)
        self.assertEqual(1, self.dll._head.value,
                         "Expected value 1 in first Node")
        self.assertEqual(1, len(self.dll), "Length should be 1")
        self.assertEqual(self.dll._head, self.dll._tail,
                         "HEAD should equal TAIL")

        prev_added_node = self.dll._head
        count = 1
        for i in range(5):
            self.dll.append_left(i)
            count += 1

            self.assertEqual(i, self.dll._head.value,
                             "Expected value " + str(i) + " in first Node")
            self.assertEqual(
                prev_added_node.value, self.dll._head.next.value,
                "Expected value" + str(prev_added_node.value) + "in next Node")
            self.assertEqual(count, len(self.dll),
                             "Length should be " + str(count))
            self.assertEqual(prev_added_node, self.dll._head.next,
                             "First node should point to second node")
            self.assertEqual(self.dll._head, prev_added_node.prev,
                             "Second node should point to this node")
            prev_added_node = self.dll._head

    def test_append(self):
        self.dll.append(1)
        self.assertEqual(self.dll._head.value, 1,
                         "Expected value 1 in first Node")
        self.assertEqual(len(self.dll), 1, "Length should be 1")
        self.assertEqual(self.dll._head, self.dll._tail,
                         "HEAD should equal TAIL")

        prev_added_node = self.dll._tail
        count = 1
        for i in range(5):
            self.dll.append(i)
            count += 1

            self.assertEqual(self.dll._tail.value, i,
                             "Expected value " + str(i) + " in last Node")
            self.assertEqual(
                self.dll._tail.prev.value, prev_added_node.value,
                "Expected value" + str(prev_added_node.value) +
                "in previous Node")
            self.assertEqual(len(self.dll), count,
                             "Length should be " + str(count))
            self.assertEqual(self.dll._tail.prev, prev_added_node,
                             "Added node should point to second last node")
            self.assertEqual(prev_added_node.next, self.dll._tail,
                             "Second Last node should point to added node")
            prev_added_node = self.dll._tail

    def test_pop(self):
        count = 5
        for i in range(count):
            self.dll.append(i)

        self.assertEqual(count, len(self.dll),
                         "Length should be " + str(count))

        # Pop from a dll with > 1 elements
        for i in range(count, 1, -1):
            value = self.dll.pop()
            count -= 1

            self.assertEqual(count, len(self.dll),
                             "Length should be " + str(count))
            self.assertEqual(i - 1, value, "Value should equal " + str(i - 1))
            self.assertIsNone(self.dll._tail.next, "TAIL.next should be None")

        # Pop from a dll with 1 element
        self.assertEqual(count, len(self.dll), "Length should be " + str(1))
        value = self.dll.pop()

        self.assertEqual(0, len(self.dll), "Length should be " + str(0))
        self.assertEqual(0, value, "Value should equal " + str(0))
        self.assertIsNone(self.dll._head, "HEAD should be None")
        self.assertIsNone(self.dll._tail, "TAIL should be None")

        # Pop from an empty dll
        node = self.dll.pop()
        self.assertIsNone(node, "Popping an empty dll returns None")

    def test_pop_left(self):
        n = count = 5
        for i in range(count):
            self.dll.append(i)

        self.assertEqual(count, len(self.dll),
                         "Length should be " + str(count))

        # Pop_left from a dll with > 1 elements
        for i in range(count - 1):
            value = self.dll.pop_left()
            count -= 1

            self.assertEqual(count, len(self.dll),
                             "Length should be " + str(count))
            self.assertEqual(i, value, "Value should equal " + str(i))
            self.assertEqual(i + 1, self.dll._head.value,
                             "Next value should equal " + str(i + 1))

        # Pop from a dll with 1 element
        self.assertEqual(count, len(self.dll), "Length should be " + str(1))
        value = self.dll.pop_left()

        self.assertEqual(0, len(self.dll), "Length should be " + str(0))
        self.assertEqual(n - 1, value, "Value should equal " + str(n - 1))
        self.assertIsNone(self.dll._head, "HEAD should be None")
        self.assertIsNone(self.dll._tail, "TAIL should be None")

        # Pop from an empty dll
        node = self.dll.pop_left()
        self.assertIsNone(node, "Popping an empty dll returns None")

    def test_find(self):
        n = 5
        for i in range(n):
            self.dll.append(i)

        self.assertEqual(3, self.dll.find(3).value, "Can find value of 3")
        self.assertIsNone(self.dll.find(10),
                          "Cannot find a node with a value of 10")

    def test_insert_after(self):
        n, m = 5, 3
        for i in range(n):
            self.dll.append(i)

        node = self.dll.find(m)
        node2 = node.next
        self.dll.insert_after(node, 10)
        self.assertEqual(10, node.next.value,
                         "Value of 10 is inserted after given node")
        self.assertEqual(node.next, node2.prev,
                         "New node is inserted before 'node2")

        expected = [i for i in range(n)]
        expected.insert(m + 1, 10)
        count = 0
        for i in self.dll:
            self.assertEqual(expected[count], i)
            count += 1

        # Insert after TAIL
        self.dll.insert_after(self.dll._tail, 11)
        self.assertEqual(11, self.dll._tail.value,
                         "Value of 11 is inserted at TAIL")
        self.assertIsNone(self.dll._tail.next, "HEAD.next is None")

    def test_insert_before(self):
        n, m = 5, 3
        for i in range(n):
            self.dll.append(i)

        node = self.dll.find(m)
        node2 = node.prev
        self.dll.insert_before(node, 10)
        self.assertEqual(10, node.prev.value,
                         "Value of 10 is inserted before 'node'")
        self.assertEqual(node2.next, node.prev,
                         "New node is inserted after 'node2")

        expected = [i for i in range(n)]
        expected.insert(m, 10)
        count = 0
        for i in self.dll:
            self.assertEqual(expected[count], i)
            count += 1

        # Insert before HEAD
        self.dll.insert_before(self.dll._head, 11)
        self.assertEqual(11, self.dll._head.value,
                         "Value of 11 is inserted at HEAD")
        self.assertIsNone(self.dll._head.prev, "HEAD.prev is None")

    def test_remove(self):
        n = 5
        for i in range(n):
            self.dll.append(i)

        # Remove existing value
        self.dll.remove(2)
        self.assertEqual(n - 1, len(self.dll),
                         "Length should be " + str(n - 1))
        self.assertIsNone(self.dll.find(2),
                          "There should be no value of 2 in the dll")
        # print(self.dll)

        # Remove non existing value
        self.dll.remove(-1)
        self.assertEqual(n - 1, len(self.dll),
                         "Length should still be" + str(n - 1))

    def test_delete(self):
        n = 5
        for i in range(n):
            self.dll.append(i)

        # Delete existing value
        node = self.dll.find(2)
        prev = node.prev
        next = node.next
        val = self.dll.delete(node)
        self.assertEqual(2, val, "Returned value should be 2")
        self.assertEqual(n - 1, len(self.dll),
                         "Length should be " + str(n - 1))
        self.assertIsNone(self.dll.find(2),
                          "There should be no value of 2 in the dll")
        self.assertEqual(next, prev.next, "Links previous to next node")
        self.assertEqual(prev, next.prev, "Links next node to previous")

        # Delete non existing value
        node = Node(2)
        val = self.dll.delete(node)
        self.assertIsNone(val, "Should return None")
        self.assertEqual(n - 1, len(self.dll),
                         "Length should still be " + str(n - 1))
        # print(self.dll)

        # Delete first node
        node = self.dll.find(0)
        next = node.next
        self.dll.delete(node)
        self.assertEqual(len(self.dll), n - 2,
                         "Length should be " + str(n - 2))
        self.assertIsNone(self.dll.find(0),
                          "There should be no value of 0 in the dll")
        for elem in self.dll:
            self.assertEqual(next.value, elem,
                             "Next should now be at the HEAD")
            break
        # print(self.dll)

        # Delete last node
        # print(self.dll)
        # node = self.dll.find(n - 1)

    def test_contains(self):
        n = 5
        for i in range(n):
            self.dll.append(i)

        # 0 - 4 should be contained in dll
        for i in range(n):
            self.assertTrue(i in self.dll, "DLL should contain " + str(i))

        # Should not contain n
        self.assertFalse(n in self.dll, "DLL should not contain" + str(n))