def RadixSort(mainlist): nummax = max(mainlist) n = len(str(nummax)) # nummax的位数确定需要循环排位的次数 mainqueue = [ Queue(), Queue(), Queue(), Queue(), Queue(), Queue(), Queue(), Queue(), Queue(), Queue() ] # 创建十个队列 for i in range(n): while mainlist: num = mainlist.pop(0) digit = num // (10**i) % 10 # 取出相应的位数 mainqueue[digit].enqueue(num) for bucket in mainqueue: while not bucket.isEmpty(): mainlist.append(bucket.dequeue()) return mainlist
def reqNum(mylist): q = Queue() mylist.sort() # 各个请求时间排序 num = [] # 用来存放请求次数 for i in mylist: q.enqueue(i) while i - q.peekfront( ) > 10000: # 队首的请求时间一定是最早的,判断与当前请求时间的差值,大于10000,则删除队首 q.remove() num.append(q.size()) #此时队列中的元素个数即为在10000毫秒内的请求次数 return num
def simulation(numseconds, pagesperminutes): # 模拟时间流逝,传入运行多长时间和打印速度 printer = Printer(pagesperminutes) printQueue = Queue() # 生成打印队列 waitingtimes = [] # 用来记录所有打印任务的等待时间 for currentsecond in range(numseconds): if newPrinttask(): printQueue.enqueue(Task(currentsecond)) if (not printer.busy()) and (not printQueue.isEmpty()): newTask = printQueue.dequeue() printer.startNext(newTask) waitingtimes.append(newTask.waittime(currentsecond)) printer.trick() averageWait = sum(waitingtimes) / len(waitingtimes) print("Average Wait %6.2f secs %3d tasks remaining." % (averageWait, printQueue.size()))
def hot_potato(name_list, num): sim_queue = Queue() for name in name_list: sim_queue.en_queue(name) while sim_queue.size() > 1: for i in range(num): # 用队尾来数数 sim_queue.en_queue(sim_queue.de_queue()) sim_queue.de_queue() return sim_queue.de_queue()
def hotPotato(namelist, num): simqueue = Queue() for name in namelist: simqueue.enqueue(name) # 把所有人名压入队列 while simqueue.size() > 1: for i in range(num): simqueue.enqueue(simqueue.dequeue()) # 循环num次,把首端人名压入尾端 simqueue.dequeue() # 然后确定该次循环中谁出列 return simqueue.dequeue()
import sys sys.path.append('../') from basic.queue import Queue q = Queue() q.enqueue('hello') q.enqueue('dog') q.enqueue(3) print(q.dequeue())
def hotPotato(namelist, num): simqueue = Queue() for name in namelist: simqueue.enqueue(name) while simqueue.size() > 1: for i in range(num): simqueue.enqueue(simqueue.dequeue()) simqueue.dequeue() return simqueue.dequeue()