Exemple #1
0
def test_queue_enqueue_two_nodes():
    """Test if able to enqueue two nodes."""
    from que_ import Queue
    test_q = Queue()
    test_q.enqueue(7)
    test_q.enqueue(8)
    assert test_q._dll.tail.data == 7
Exemple #2
0
def test_queue_len_function():
    """Test that len function returns queue size."""
    from que_ import Queue
    q = Queue()
    q.enqueue('one')
    q.enqueue('two')
    assert len(q) == 2
Exemple #3
0
def test_peek():
    """Test if next value to be removed is accurate."""
    from que_ import Queue
    test_q = Queue()
    test_q.enqueue(99)
    test_q.enqueue(5)
    assert test_q.peek() == 99
Exemple #4
0
def test_queue_peek_with_data():
    """Test the queue peek method displays the tail."""
    from que_ import Queue
    q = Queue()
    q.enqueue('one')
    q.enqueue('two')
    assert q.peek() == 'one'
Exemple #5
0
def test_queue_size_method():
    """Test that queue size method returns size."""
    from que_ import Queue
    q = Queue()
    q.enqueue('one')
    q.enqueue('two')
    assert q.size() == 2
Exemple #6
0
def test_queue_constructor_with_empty_iterable():
    """Test that a Queue created with an empty iterable is empty."""
    from que_ import Queue
    q = Queue([])
    assert q.back is None
    assert q.front is None
    assert q._values.length == 0
Exemple #7
0
def test_dequeue_method():
    """Remove a node in the proper sequence."""
    from que_ import Queue
    q = Queue()
    q.enqueue('one')
    q.enqueue('two')
    assert q.dequeue() == 'one'
def radix_sort(input_list):
    """Radix sort."""
    if not isinstance(input_list, (list, tuple)):
        raise ValueError('input takes list/tuple only')

    if isinstance(input_list, (tuple)):
        input_list = list(input_list)

    if not all(isinstance(val, (int, float)) for val in input_list):
        raise ValueError('all items in list must be a number')

    radix_list = input_list[:]  # prevent changing original list
    neg = False  # if neg value exist in list
    for num in radix_list:
        if num < 0:
            neg = True
            temp = [int(num + 10e10) for num in radix_list]
            radix_list = temp
            break

    str_inputs = [str(val) for val in radix_list]
    max_val = max(str_inputs, key=len)
    q_set = [Queue() for _ in range(10)]
    indx = 1

    for indx in range(1, len(max_val) + 1):
        _populate_sets(str_inputs, indx, q_set)
        str_inputs = _dequeue_sets(q_set)

    if neg:
        return [int(int(val) - 10e10) for val in str_inputs]
    else:
        return [int(val) for val in str_inputs]
Exemple #9
0
def test_queue_constructor_with_iterable(itr):
    """Test that a queue created with an iterable contains all items."""
    from que_ import Queue
    q = Queue(itr)
    assert q.back.val == itr[-1]
    assert q.front.val == itr[0]
    assert q._values.length == len(itr)
def test_head_tail_of_list_of_len_1():
    """Test that in a one-node list, the node is head and tail."""
    from que_ import Queue
    q = Queue()
    q.enqueue(5)
    assert q.linked_list.head
    assert q.linked_list.tail
def q_20():
    """Making one queue instance with len of 20 per test."""
    from que_ import Queue
    q = Queue()
    for num in range(20):
        q.enqueue(num)
    return q
Exemple #12
0
def test_dequeue_node():
    """Test if able to dequeue a node."""
    from que_ import Queue
    test_q = Queue()
    test_q.enqueue(9)
    test_q.enqueue(10)
    assert test_q.dequeue() == 9
Exemple #13
0
    def breadth_first(self):
        """
        Traversal through bst with breadth first approach.

        Look at each node at current depth before going on to next level.
        """
        from que_ import Queue
        q = Queue()
        nodes = []
        q.enqueue(self.root)
        if self.root is None:
            raise ValueError("The bst is empty.")

        def _recur_breadth_first():
            """Recursive helper function for breadth first."""
            try:
                current = q.dequeue()
                nodes.append(current.val)
                if current.left:
                    q.enqueue(current.left)
                if current.right:
                    q.enqueue(current.right)
                _recur_breadth_first()
            except IndexError:
                return

        _recur_breadth_first()
        for val in nodes:
            yield val
Exemple #14
0
 def test_7(self):
     """ Check that length returns the size of queue """
     q = Queue()
     n1 = Node(1)
     n2 = Node(2)
     q.enqueue(n1)
     q.enqueue(n2)
     self.assertEqual(q.length(), 2)
Exemple #15
0
def test_dequeue_node_length():
    """Test if able to dequeue a node."""
    from que_ import Queue
    test_q = Queue()
    test_q.enqueue(9)
    test_q.enqueue(10)
    test_q.dequeue()
    assert test_q.size == 1
def test_if_peek_needs_no_inputs():
    """Test that an unnecessary parameter will raise a TypeError."""
    import pytest
    from que_ import Queue
    q = Queue()
    q.enqueue(5)
    with pytest.raises(TypeError):
        q.peek(5)
Exemple #17
0
def test_dequeue_head_is_tail_with_one():
    """Test if head == tail if only one node."""
    from que_ import Queue
    test_q = Queue()
    test_q.enqueue(99)
    test_q.enqueue(5)
    test_q.dequeue()
    assert test_q._dll.tail == test_q._dll.head
Exemple #18
0
 def test_5(self):
     """ Check that peek gives the value of the top node """
     q = Queue()
     n1 = Node(1)
     n2 = Node(2)
     q.enqueue(n1)
     q.enqueue(n2)
     q.peek()
     self.assertEqual(q.top._value, 1)
 def insert(self, data, pri=0):
     """Will insert a value in the priority queue with a priority of 0 or the optional priority input."""
     from que_ import Queue
     if not data:
         raise ValueError('need value to push')
     try:
         self.q[pri]
     except KeyError:
         self.q[pri] = Queue()
     self.q[pri].enqueue(data)
Exemple #20
0
def test_dequeue_one_item_from_any_length_queue(itr):
    """Test that dequeue item removes value from head of Queue."""
    from que_ import Queue
    q = Queue(itr)
    x = q.dequeue()
    assert x == itr[0]
    assert q.back.val == itr[-1]
    assert q.front.val == itr[1]
    assert q.front.nxt is None
    assert q._values.length == len(itr) - 1
Exemple #21
0
 def test_3(self):
     """ check that nodes are appending to the bottom of the queue, not the top """
     q = Queue()
     n1 = Node(1)
     n2 = Node(2)
     n3 = Node(3)
     q.enqueue(n1)
     q.enqueue(n2)
     q.enqueue(n3)
     self.assertEqual(q.top._value, 1)
Exemple #22
0
 def test_4(self):
     """ check that deque turns the second node into the top node """
     q = Queue()
     n1 = Node(1)
     n2 = Node(2)
     n3 = Node(3)
     q.enqueue(n1)
     q.enqueue(n2)
     q.enqueue(n3)
     q.dequeue()
     self.assertEqual(q.top._value, 2)
Exemple #23
0
 def breadth_first(self):
     """Return values breadth first."""
     q = Queue()
     q.enqueue(self.root)
     while len(q) > 0:
         node = q.dequeue()
         if node.left:
             q.enqueue(node.left)
         if node.right:
             q.enqueue(node.right)
         self.list.append(node.value)
     return self.list
Exemple #24
0
def test_dequeue_multiple_items_from_any_length_queue(itr):
    """Test that dequeue items removes head from Queue."""
    from que_ import Queue
    from random import randint
    q = Queue(itr)
    num = randint(2, len(itr) - 1)
    for _ in range(num):
        x = q.dequeue()
    assert x == itr[num - 1]
    assert q.back.val == itr[-1]
    assert q.front.val == itr[num]
    assert q.front.nxt is None
    assert q._values.length == len(itr) - num
 def breadth(self):
     from que_ import Queue
     return_list = []
     next_up = Queue()
     next_up.enqueue(self._root)
     while next_up.size() > 0:
         temp = next_up.dequeue().value
         if temp.left:
             next_up.enqueue(temp.left)
         if temp.right:
             next_up.enqueue(temp.right)
         return_list.append(temp.value)
     for value in return_list:
         yield value
Exemple #26
0
    def breadth_first(self):
        """Return a generator that returns values tree breadth first order."""
        if self.size_count == 0:
            raise IndexError("Cannot traverse empty tree.")

        breadth_list = Queue()
        breadth_list.enqueue(self.root)
        yield breadth_list.peek().val

        while breadth_list.peek():
            if breadth_list.peek().left:
                breadth_list.enqueue(breadth_list.peek().left)
                yield breadth_list.peek().left.val
            if breadth_list.peek().right:
                breadth_list.enqueue(breadth_list.peek().right)
                yield breadth_list.peek().right.val
            breadth_list.dequeue()
Exemple #27
0
 def make_family(self, node):
     """Make a list of family members for node."""
     family = Queue()
     family.enqueue(node)
     if node.parent:
         family.enqueue(node.parent)
     if node.parent.parent:
         parent = node.parent
         grandparent = node.parent.parent
         family.enqueue(grandparent)
         if grandparent.left:
             if grandparent.left != parent:
                 family.enqueue(grandparent.left)
         if grandparent.right:
             if grandparent.right != parent:
                 family.enqueue(grandparent.right)
     self.recoloring(family)
Exemple #28
0
def breadth_first_traversal(graph, start):
    """Traverse a graph by breadth."""
    if not isinstance(graph, Graph):
        raise TypeError('Must provide graph.')
    if not graph.has_node(start):
        raise KeyError('Node not in graph.')
    peeped = []
    queue = Queue()
    queue.enqueue(start)
    while len(queue) > 0:
        node = queue.dequeue()
        if node not in peeped:
            peeped.append(node)
        for neighbor in graph._graph[node]:
            if neighbor not in peeped:
                queue.enqueue(neighbor)
    return peeped
 def insert(self, val, priority=None):
     """."""
     if not priority:
         priority = self.lowest
     if priority < self.lowest:
         self.lowest = priority
     for queue in self._priority:
         if priority == queue.priority:
             queue.enqueue(val)
             return
     q = Queue(priority)
     q.enqueue(val)
     for idx, queue in enumerate(self._priority):
         if priority > self._priority[idx].priority:
             self._priority.insert(idx, q)
             return
     self._priority.append(q)
Exemple #30
0
    def breadth_first_traversal(self, val):
        """Return a list of all nodes connected to given start pointbased on a breadth first algorithm."""
        from que_ import Queue
        seen = []
        next_up = Queue()
        try:
            while True:
                if val not in seen:
                    seen.append(val)
                    for i in self.graph_dict[val]:
                        next_up.enqueue(i)
                if next_up.size() == 0:
                    break
                val = next_up.dequeue()[0]
            return seen
        except KeyError:

            raise KeyError('Given value does not exist.')