def build_tree(frequency_map): frequency_map = sort_map_by_value(frequency_map) nodes = [] for [symbol, f] in frequency_map.items(): node = Node.symbol(f, symbol) nodes.append(node) q1 = Queue(nodes) q2 = Queue() def get_min(q1, q2): if less(q1.front(), q2.front()): return q1.pop() else: return q2.pop() while q1.len() + q2.len() >= 2: x = get_min(q1, q2) y = get_min(q1, q2) s = Node.combine(x, y) q2.push(s) return q2.front()
def test_multiple_queues(self): q1 = Queue() q2 = Queue() q1.push(1) q2.push(2) self.assertEqual(get_array(q1), [1]) self.assertEqual(get_array(q2), [2])
def test_basic(self): q1 = Queue() q1.put("a") v = q1.get() self.assertEqual(v, "a") threading.Thread(target=self.pub, args=(q1, "b")).start() v = q1.get(5) self.assertEqual(v, "b")
def simulation(num_seconds, pages_per_minute): lab_printer = Printer(pages_per_minute) print_queue = Queue() waiting_times = [] for current_second in range(num_seconds): if new_print_task(): task = Task(current_second) print_queue.enqueue(task) if not lab_printer.busy() and not print_queue.is_empty(): next_task = print_queue.dequeue() waiting_times.append(next_task.wait_time(current_second)) lab_printer.start_next(next_task) lab_printer.tick() average_wait = sum(waiting_times) / len(waiting_times) print("Average Wait %6.2f secs %3d tasks remaining" % (average_wait, print_queue.size()))
def hot_potato(namelist, num): """ Simulation of hot potato game. :param namelist: a list of names :param num: a constant used for counting :return: name of the last person remaining after repetitive counting by num Assume that the person holding the potato will be at the front of the queue. Upon passing the potato, the simulation will simply dequeue and then immediately enqueue that person, putting her at the end of the line. She will then wait until all the others have been at the front before it will be her turn again. After num dequeue/enqueue operations, the person at the front will be removed permanently and another cycle will begin. This process will continue until only one name remains (the size of the queue is 1). """ simple_queue = Queue() for name in namelist: simple_queue.enqueue(name) while simple_queue.size() > 1: for i in range(num): simple_queue.enqueue(simple_queue.dequeue()) simple_queue.dequeue() return simple_queue.dequeue()
test(Queue,lst) print('\n') brh = BinaryHeap() brh_time = timer() for dst, u, v in lst: brh.add([dst, u, v]) brh_time = timer() - brh_time blh = BinomialHeap() blh_time = timer() for dst, u, v in lst: blh.add([dst, u, v]) blh_time = timer() - blh_time sq = Queue() q_time = timer() for dst, u, v in lst: sq.add([dst, u, v]) q_time = timer()-q_time print(f"Binary Heap: {brh_time}\nBinomial Heap: {blh_time}\n" + f"Simple Queue: {q_time}") # ============================================================================= # M=[[0, 3, 5, np.inf], # [2, 0, np.inf, 4], # [1, np.inf, 0, 6], # [np.inf, 8, 2, 0]] # M=np.array(M) # H=Graph(M,[1,2,1,2],[2,2,1,1])
def test_operations(self): q = Queue() self.assertTrue(q.is_empty()) self.assertEqual(q.front(), None) self.assertEqual(q.pop(), None) q.push(1) self.assertFalse(q.is_empty()) self.assertEqual(q.front(), 1) q.push(2) self.assertEqual(q.front(), 1) val = q.pop() self.assertEqual(val, 1) self.assertEqual(q.front(), 2) val = q.pop() self.assertEqual(val, 2) self.assertTrue(q.is_empty())
def test_initialized_queue(self): init = [1, 2, 3, 4, 5] q = Queue([1, 2, 3, 4, 5]) arr = get_array(q) self.assertEqual(arr, init)