Пример #1
0
 def test_queue_dequeue(self):
     capacity = 10
     q = Queue(capacity=capacity)
     for i in range(capacity):
         q.enqueue(i)
     for i in range(capacity):
         self.assertEqual(i, q.dequeue())
Пример #2
0
 def test_front_of_non_empty_queue_returns_first_in_value(self):
     queue = Queue()
     queue.enqueue(0)
     queue.enqueue(1)
     queue.enqueue(2)
     assert queue.front() == 0
     queue.dequeue()
     assert queue.front() == 1
Пример #3
0
 def test_has_more(self):
     capacity = 10
     q = Queue(capacity=capacity)
     self.assertEqual(False, q.has_more())
     for i in range(capacity):
         q.enqueue(i)
         self.assertEqual(True, q.has_more())
     for i in range(capacity):
         self.assertEqual(True, q.has_more())
         q.dequeue()
     self.assertEqual(False, q.has_more())
Пример #4
0
    def test_queue_size_is_reduced_by_dequeue(self):
        queue = Queue()
        queue.enqueue(0)
        queue.enqueue(1)
        queue.enqueue(2)
        assert len(queue) == 3

        queue.dequeue()
        assert len(queue) == 2
        queue.dequeue()
        queue.dequeue()
        assert len(queue) == 0
 def test_dequeue(self):
   """
   Test for dequeue method
   """
   queue = Queue()
   self.assertEqual(queue.dequeue(), None)
   queue.enqueue(1)
   queue.enqueue(2)
   queue.enqueue(3)
   self.assertEqual(queue.dequeue(), 1)
   self.assertEqual(queue.size(), 2)
 def test_peek(self):
   """
   Test for peek method
   """
   queue = Queue()
   self.assertEqual(queue.peek(), None)
   queue.enqueue(1)
   queue.enqueue(2)
   queue.enqueue(3)
   self.assertEqual(queue.peek(), 1)
   self.assertEqual(queue.size(), 3)
class CashRegister:

    def __init__(self) -> None:
        self.clients = Queue()

    def __str__(self) -> str:
        return f"{self.clients}"

    def add_client(self, client: Client):
        self.clients.push(client)
        # print(f"Added client: {client}")

    def process(self) -> Client:
        # print(f"Removed client")
        return self.clients.pop()
 def test_enqueue(self):
   """
   Test for enqueue method
   """
   queue = Queue()
   self.assertEqual(queue.size(), 0)
   queue.enqueue(1)
   queue.enqueue(2)
   queue.enqueue(3)
   self.assertEqual(queue.size(), 3)
Пример #9
0
    def _level_order_print(self, start):
        if start is None:
            return

        queue = Queue()
        queue.enqueue(start)

        traversal = ""
        while len(queue) > 0:
            traversal += str(queue.peek().value) + "-"
            node = queue.dequeue()

            if node.left:
                queue.enqueue(node.left)
            if node.right:
                queue.enqueue(node.right)

        return traversal
def play_hot_potato_game(items, reps):
    queue = Queue()

    # O(n)
    for item in items:
        queue.enqueue(item)

    # O(n - 1)
    while queue.size() > 1:
        # O(#reps)
        for i in range(reps):
            first = queue.dequeue()
            print(first)

            queue.enqueue(first)
        print('-' * 10)
        print('Removing {}'.format(queue.dequeue()))

    return queue.dequeue()
    def test_enqueue(self):
        print('Test: test_enqueue')
        queue = Queue()

        print('Test: None input')
        self.assertRaises(ValueError, queue.enqueue, None)

        print('Test: Enqueue to an empty queue')
        queue.enqueue(1)
        self.assertEqual(queue._front.data, 1)
        self.assertEqual(queue._rear.data, 1)

        print('Test: General case')
        queue.enqueue(2)
        self.assertEqual(queue._rear.data, 2)
        queue.enqueue(3)
        self.assertEqual(queue._rear.data, 3)
        queue.enqueue(4)
        self.assertEqual(queue._rear.data, 4)
        print('Success: test_enqueue')
Пример #12
0
def bfs(graph, start):
    start.set_distance(0)
    start.set_predecessor(None)
    vertex_queue = Queue()
    vertex_queue.enqueue(start)

    while vertex_queue.size() > 0:
        current_vertex = vertex_queue.dequeue()
        for nbr in current_vertex.get_connections():
            if nbr.get_color() == 'white':
                nbr.set_color('gray')
                nbr.set_distance(current_vertex.get_distance() + 1)
                nbr.set_predecessor(current_vertex)
                vertex_queue.enqueue(nbr)
        current_vertex.set_color('black')
Пример #13
0
 def test_queue_size_matches_number_of_enqueues(self):
     queue = Queue()
     assert len(queue) == 0
     queue.enqueue(0)
     assert len(queue) == 1
     queue.enqueue(1)
     queue.enqueue(2)
     assert len(queue) == 3
 def test_size(self):
   """
   Test for size method
   """
   queue = Queue()
   self.assertEqual(queue.size(), 0)
   queue.enqueue(1)
   self.assertEqual(queue.size(), 1)
 def test_is_empty(self):
   """
   Test for is_empty method
   """
   queue = Queue()
   self.assertEqual(queue.is_empty(), True)
   queue.enqueue(1)
   self.assertEqual(queue.is_empty(), False)
    def test_is_empty(self):
        print('Test: test_is_empty')
        queue = Queue()

        print('Test: Empty queue')
        self.assertEqual(queue.is_empty(), True)

        print('Test: Not an empty queue')
        queue.enqueue(1)
        self.assertEqual(queue.is_empty(), False)
        print('Success: test_is_empty')
    def test_peek(self):
        print('Test: test_peek')
        queue = Queue()

        print('Test: Peek to an empty queue')
        self.assertEqual(queue.peek(), None)

        print('Test: General case')
        queue.enqueue(1)
        queue.enqueue(2)
        self.assertEqual(queue.peek(), 1)
        print('Success: test_peek')
Пример #18
0
    def _reverse_level_order_print(self, start):
        if start is None:
            return

        queue = Queue()
        stack = Stack()
        queue.enqueue(start)

        traversal = ""
        while len(queue) > 0:
            node = queue.dequeue()

            stack.push(node)

            if node.right:
                queue.enqueue(node.right)
            if node.left:
                queue.enqueue(node.left)

        while len(stack) > 0:
            node = stack.pop()
            traversal += str(node.value) + "-"

        return traversal
Пример #19
0
 def test_get_rear_of_empty_queue_raises_empty_queue_exception(self):
     queue = Queue()
     with raises(Queue.EmptyQueueException):
         queue.rear()
Пример #20
0
 def test_len_retrieves_queue_size(self):
     queue = Queue()
     assert len(queue) == queue.size()
Пример #21
0
 def test_initialized_queue_has_size_0(self):
     queue = Queue()
     assert queue.size() == 0
Пример #22
0
 def test_exception(self):
     q = Queue(capacity=1)
     q.enqueue(1)
     self.assertRaises(Exception, q.enqueue, (1, ))
     q.dequeue()
     self.assertRaises(Exception, q.dequeue)
Пример #23
0
 def test_queue_is_not_empty_after_enqueue(self):
     queue = Queue()
     queue.enqueue(0)
     assert not queue.empty()
Пример #24
0
 def test_initialization(self):
     queue = Queue()
     assert isinstance(queue, Queue)
import time

from data_structures.queue.queue import Queue, QueueDeque

queue1 = Queue()
queue2 = QueueDeque()

start = time.time()
for i in range(100000):
    queue1.push("ITEM")

for i in range(100000):
    queue1.pop()

stop = time.time()

print(stop - start)

start = time.time()

for i in range(100000):
    queue2.push("ITEM")

for i in range(100000):
    queue2.pop()

stop = time.time()

print(stop - start)
Пример #26
0
 def test_rear_of_non_empty_queue_returns_last_in_value(self):
     queue = Queue()
     queue.enqueue(0)
     assert queue.rear() == 0
     queue.enqueue(1)
     assert queue.rear() == 1
     queue.enqueue(2)
     assert queue.rear() == 2
Пример #27
0
 def test_initialized_queue_is_empty(self):
     queue = Queue()
     assert queue.empty()
Пример #28
0
 def test_get_front_of_empty_queue_raises_empty_queue_exception(self):
     queue = Queue()
     with raises(Queue.EmptyQueueException):
         queue.front()
Пример #29
0
    def test_dequeue_empty_queue_raises_empty_queue_exception(self):
        queue = Queue()
        with raises(Queue.EmptyQueueException):
            queue.dequeue()

        queue = Queue()
        queue.enqueue(0)
        queue.dequeue()
        with raises(Queue.EmptyQueueException):
            queue.dequeue()
Пример #30
0
def fix_object_under_test() -> Queue:
    return Queue()