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())
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
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())
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)
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')
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')
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')
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
def test_get_rear_of_empty_queue_raises_empty_queue_exception(self): queue = Queue() with raises(Queue.EmptyQueueException): queue.rear()
def test_len_retrieves_queue_size(self): queue = Queue() assert len(queue) == queue.size()
def test_initialized_queue_has_size_0(self): queue = Queue() assert queue.size() == 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)
def test_queue_is_not_empty_after_enqueue(self): queue = Queue() queue.enqueue(0) assert not queue.empty()
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)
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
def test_initialized_queue_is_empty(self): queue = Queue() assert queue.empty()
def test_get_front_of_empty_queue_raises_empty_queue_exception(self): queue = Queue() with raises(Queue.EmptyQueueException): queue.front()
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()
def fix_object_under_test() -> Queue: return Queue()