def test_queue_isEmpty(): test = Queue() assert (test.isEmpty == True) test.enqueue(3) assert (test.isEmpty == False) test.dequeue() assert (test.isEmpty == True)
def test_size(): test = Queue() test.enqueue(3) assert (test.size() == 1) test.enqueue(34) assert (test.size() == 2) test.dequeue() assert (test.size() == 1)
def hotpotato(namelist, number): namequeue = Queue() for name in namelist: namequeue.enqueue(name) while int(namequeue.size()) > 1: for i in range(number): namequeue.enqueue(namequeue.dequeue()) namequeue.dequeue() return namequeue.dequeue()
class QueueTest(unittest.TestCase): def setUp(self): self.q = Queue() def tearDown(self): while not self.q.isEmpty(): self.q.dequeue() def test_enqueue(self): value = 'A' self.q.enqueue(value) self.assertEqual(1, self.q.size())
class printer_q(): ''' Printer Queue ''' def __init__(self, speed=20): self._queue = Queue() self._finish = [] self.speed = speed self.t_time = 0 self.current_job = None self._done = False def add_job(self, job): print('add printing job with pages: ', job.get_pages()) self._queue.enqueue(job) def work(self): ''' Work function Start to work until the queue is empty ''' if self._done: return self._done elif self._queue.isEmpty: self._done = True return self._done # We need to do work if self.current_job is None: if self._queue.isEmpty: self._done = True return True else: self._done = False self.current_job = self._queue.dequeue() if (self.current_job.printing(self.speed) <= 0): # Loop over all remaining queues for i in self._queue._contents: i.add_wait_time() self._finish.append(self.current_job) self.current_job = None self._done = False return self._done else: for i in self._queue._contents: i.add_wait_time() self._done = False return self._done def get_total_time(self): self.t_time = sum([x.get_wait_time() for x in self._finish]) / len(self._finish) return self.t_time
def BFS(network, originUser): neighbors = {} visited = {} for user in network: visited[user] = False neighbors[user] = [] visited[originUser] = True queue = Queue() queue.enqueue(originUser) while not queue.isEmpty(): u = queue.dequeue() for v in network[u]['connections']: if not visited[v]: visited[v] = True queue.enqueue(v) neighbors[u].append(v) return neighbors #def DFS():
def test_dequeue(): test = Queue() test.enqueue(3) test.enqueue(4) assert (test.dequeue() == 3) assert (test.dequeue() == 4)