def hot_potato(namelist, num): circle = MyQueue() for name in namelist: circle.add(name) while len(circle) != 1: for i in range(num): circle.add(circle.pop()) circle.pop() return circle.pop()
def simulation(total_seconds, pages_per_pages): """ 模拟运行环境 """ printer = Printer(pages_per_pages) printer_queue = MyQueue() time_per_task = [] for current_second in range(total_seconds): if new_task(): printer_queue.add(Task(current_second)) if (len(printer_queue) != 0) and (not printer.isBusy()): current_task = printer_queue.pop() printer.startTask(current_task) time_per_task.append(current_task.waitTime(current_second)) printer.tick() avg_time_per_task = sum(time_per_task) / len(time_per_task) print( 'Average time {:6.2f} secs, total {} tasks, {} tasks remaining'.format( avg_time_per_task, len(time_per_task), len(printer_queue), ))
def bfs(graph, start): """ breadth first search """ q = MyQueue() visited_id = set() # avoid visit repeated v = graph.get_vertex(start) if v: visited_id.add(v.id) # add start vertex # add v.nbr in queue for i in v.get_connections(): q.add(i) visited_id.add(i.id) else: raise ValueError('Error! start vertex {start} is not in Graph.') while q: # bfs traverse current_vert = q.pop() print(current_vert.id) # print vertex id by bfs for i in current_vert.get_connections(): if i.id not in visited_id: q.add(i) visited_id.add(i.id)
from my_queue import MyQueue k1 = MyQueue(4) k1.add(3) k1.add(3123) k1.add(3) k1.add(123) k1.add(4) k1.add(0) k1.add(2) k1.add(1) print(k1.size()) k1.remove() k1.remove() k1.remove() k1.remove() k1.remove() k1.remove()