def simulation(numSeconds, pagesPerMinute): # create a new printer object labprinter = Printer(pagesPerMinute) # create a queue object printQueue = Queue() # create a list type of object, for storing all waiting times for each task waitingtimes = [] # unit is based on second, and numSeconds is the total time # for simulation, create a loop for simulation for currentSecond in range(numSeconds): # newprinttaask decides whether we want to create a new task # see detail in newPrintTask function defined later if newPrintTask(): task = Task(currentSecond) # if there is a new task, put that new task to # the end of the queue printQueue.enqueue(task) if (not labprinter.busy()) and (not printQueue.isEmpty()): # when printer is not busy or queue is empty # start to process next task from the top of the task queue nexttask = printQueue.dequeue() # meanwhile add the waiting time to the end of the time lise waitingtimes.append(nexttask.waitTime(currentSecond)) # then start the next task pop out from the queue labprinter.startNext(nexttask) # keep counting the time till the end of the task labprinter.tick() averageWait = sum(waitingtimes) / len(waitingtimes) print("Average Wait %6.2f secs %3d tasks remaining." % (averageWait, printQueue.size()))
def pig_latin_converter(word): if type(word) != str: raise TypeError('Argument should be a string') my_simulation = Queue() to_lower = word.lower() modified = list(to_lower) pos = 0 found = True if modified[0] in ['a', 'e', 'i', 'o', 'u']: return word + 'way' else: while pos <= len(word) and found: if modified[pos] in ['a', 'e', 'i', 'o', 'u']: found = False else: my_simulation.enqueue(modified[pos]) pos += 1 rounds = my_simulation.size() simulator_stack = Stack() for i in modified: simulator_stack.push(i) diff = len(word) - rounds fixed = [] for j in range(diff): fixed.append(simulator_stack.pop()) fixed.reverse() final_word = '' to_fix_with = [] while not my_simulation.isEmpty(): to_fix_with.append(my_simulation.dequeue()) final_word = ''.join(fixed) + ''.join(to_fix_with) + 'ay' return final_word
def hotPotato(namelist, k): """ Simulate the children's game Hot Potato. In this game children line up in a circle and pass an item from neighbor to neighbor as fast as they can. At a certain point in the game, the action is stopped and the child who has the item (the potato) is removed from the circle. :param namelist: A list of children's name involved in this game, required to be nonempty. :param k: the position of child to be removed from the circular namelist counting from the front of the queue (The front one is assumed to be at position 0). :return: the name of the last person remaining after repetitive counting by k. """ simqueue = Queue() for name in namelist: simqueue.enqueue(name) while simqueue.size() > 1: for i in range(k): simqueue.enqueue(simqueue.dequeue()) simqueue.dequeue() return simqueue.dequeue()
def check(): # Code for checking the results ans_check = input('Do you want to print the result in dorder they were played or sorted? \n \ [Enter "o" for in order and "s" for sorted]\n') if ans_check == 'o': f = open('result.txt','r') # Another way to operate file I/O result = Queue() for line in open('result.txt'): line = line.strip() # To make the output more clear result.enqueue(line) while result.size()>1: print(result.dequeue()) f.close() if ans_check == 's': f = open('result.txt','r') result = [] # Read lines and store it as a list for line in open('result.txt'): line = line.strip() result.append(line) result.sort() # Sorted for item in result: print(item) f.close()
def murderPossibility(band, enemy, enemyX, enemyY, enemyId, enemyDirection, me, meId, distance): searchGrid = { 0: [(1, 0, 0), (0, -1, 3), (0, 1, 1)], 1: [(0, 1, 1), (1, 0, 0), (-1, 0, 2)], 2: [(-1, 0, 2), (0, 1, 1), (0, -1, 3)], 3: [(0, -1, 3), (1, 0, 0), (-1, 0, 2)] } searchList = [(enemyX, enemyY)] searchQueue = Queue() searchQueue.enqueue((enemyX, enemyY, 0, enemyDirection)) while True: currentPosition = searchQueue.dequeue() currentX = currentPosition[0] currentY = currentPosition[1] currentStep = currentPosition[2] currentDirection = currentPosition[3] if currentStep >= distance + 5: return False step = currentStep + 1 for coordinate in searchGrid[currentDirection]: horizon = currentX + coordinate[0] vertical = currentY + coordinate[1] newDirection = coordinate[2] if horizon >= 0 and horizon <= 100 and vertical >= 0 and vertical <= 101: if band[horizon][vertical] == meId and step < distance + 5: return True elif band[horizon][vertical] != enemyId and ( (horizon, vertical) not in searchList): searchQueue.enqueue( (horizon, vertical, step, newDirection)) searchList.append((horizon, vertical))
def hotPotato(namelist, num): """烫手山芋问题 类似于约瑟夫问题,游戏规则是孩子们围成一个圈, 然后从队首开始报数,报到指定数字的孩子出局, 最后只剩下一个人 参数: namelist: 参与游戏的孩子人名的列表 num: 报数的数字 返回值: simqueue.dequeue(): String 胜出的孩子 """ simqueue = Queue() # 把所有人名放进队列 for name in namelist: simqueue.enqueue(name) # 当队列人数大于1的时候执行,因为每一轮报数都会有一个人出队,长度-1 while simqueue.size() > 1: # 每一次报数过程中,队首的人出队然后重新进队 for i in range(num): simqueue.enqueue(simqueue.dequeue()) # 每一轮报数完后队首的人出队,队列长度-1 simqueue.dequeue() return simqueue.dequeue()
def safe_postion(num_soldiers): queue_sol = Queue() # Declare an Empty Queue # Adding the list-num_soldiers to Queue using push for name in num_soldiers: queue_sol.enqueue(name) # looping through the soldires and killing the next to it # list_a =[] print("Original Circle:%s " % (queue_sol.items)) while queue_sol.size() > 1: for i in range(1): k = queue_sol.enqueue(queue_sol.dequeue()) print('-' * 30) #s = s.join(queue_sol.items) print("Remaining Soldier:", "".join(queue_sol.items)) #print("Remaining Soldier:%s" % (queue_sol.items)) print('-' * 30) #print('%s Kills: '%(queue_sol.items[0])) n = queue_sol.dequeue() print(queue_sol.items[0] + ' Kills: ' + n) time.sleep(1) return queue_sol.dequeue()
def simulation(numSeconds, pagesPerMinute): """ Simulate the printing tasks managed by the shared printer in a computer science lab. :param numSeconds: the total time for the printer to run :param pagesPerMinute: the printing speed of the printer """ labprinter = Printer(pagesPerMinute) printQueue = Queue() waitingtimes = [] for currentSecond in range(numSeconds): if newPrintTask(): task = Task(currentSecond) printQueue.enqueue(task) if (not labprinter.busy()) and (not printQueue.isEmpty()): nexttask = printQueue.dequeue() waitingtimes.append(nexttask.waitTime(currentSecond)) labprinter.startNext(nexttask) labprinter.tick() averageWait = sum(waitingtimes) / len(waitingtimes) print("Average Wait %6.2f secs %3d tasks remaining." % (averageWait, printQueue.size()))
def hotPotato(namelist, num): #参加游戏的人名列表namelist,传num次数 simqueue = Queue() #创建队列queue for name in namelist: simqueue.enqueue(name) #队列存放namelist里面参加游戏的人名 while simqueue.size() > 1: for i in range(num): simqueue.enqueue(simqueue.dequeue()) #传递过程:位于队首的人出列,紧接着从队尾入队。 simqueue.dequeue() #每一轮移除一次队首的人 return simqueue.dequeue() #while循环传递num次之后,最终返回最后唯一、剩下的1人
def send_flowers(name_list, num): simqueue = Queue() for name in name_list: simqueue.enqueue(name) while simqueue.size() > 1: for i in range(num): simqueue.enqueue(simqueue.dequeue()) simqueue.dequeue() return simqueue.dequeue()
def send_flower(name_list, num): q = Queue() for name in name_list: q.enqueue(name) while q.size() > 1: for i in range(num): q.enqueue(q.dequeue()) print(q.dequeue()) return q.dequeue()
def hotPotato(names, num): q = Queue() for ii in names: q.enqueue(ii) while q.size() != 1: for ii in range(num): q.enqueue(q.dequeue()) q.dequeue() return 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()
def send_flowers(name_list, num): simqueue = Queue() for name in name_list: simqueue.enqueue(name) while simqueue.size() > 1: for i in range(num): # 把移除的前六个人重新放入队伍 simqueue.enqueue(simqueue.dequeue()) print(simqueue.dequeue()) return simqueue.dequeue()
def hotpotato(nameList, num): q = Queue() for i in nameList: q.enqueue(i) while q.size() > 1: for _ in range(num): v = q.dequeue() q.enqueue(v) print(q.dequeue()) print(f"the last one is: {q.dequeue()}")
def HotPotato(namelist: List[str], repetitions: int) -> List[str]: queue = Queue() for name in namelist: queue.enqueue(name) for rep in range(repetitions): person = queue.dequeue() queue.enqueue(person) return queue.dequeue()
def hotpotato(namelist, num): aqueue = Queue() for name in namelist: aqueue.enqueue(name) while aqueue.size() > 1: for i in range(num): aqueue.enqueue(aqueue.dequeue()) aqueue.dequeue(), return aqueue.dequeue()
def hotPotato(num, names): potatoQueue = Queue() for name in names: potatoQueue.enqueue(name) while potatoQueue.size() > 1: for i in range(num): potatoQueue.enqueue(potatoQueue.dequeue()) potatoQueue.dequeue() return potatoQueue.dequeue()
def hotPotato(namelist, num): q = Queue() for name in namelist: q.enqueue(name) while q.size() > 1: for i in range(num): q.enqueue(q.dequeue()) q.dequeue() return q.dequeue()
def hotpotato(namelist, num): simqueue = Queue() for name in namelist: simqueue.enqueue(name) # 现在的队列是["Brad", "Kent", "Jane", "Susan", "David", "Bill"] # 从Bill开始数,逆时针 while simqueue.size() > 1: for i in range(num): simqueue.enqueue(simqueue.dequeue()) simqueue.dequeue() return simqueue.dequeue()
def hotPotato(namelist,num): s = Queue() for one in namelist: s.enqueue(one) while s.size() > 1: for i in range(num): tmp = s.dequeue() s.enqueue(tmp) tmp = s.dequeue() return s.dequeue()
def hot_potato(names, num): s = Queue() for name in names: s.enqueue(name) while s.size() > 1: for i in range(num): s.enqueue(s.dequeue()) s.dequeue() return s.dequeue()
def hotPotato(namelist, num): simqueue = Queue() #taken from previous 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()
def hotPotato(namelist, num): simqueue = Queue() for name in namelist: simqueue.enqueue(name) #move the names from the list to the queue while simqueue.size() > 1: for i in range(num): simqueue.enqueue(simqueue.dequeue()) simqueue.dequeue() #person eliminated return simqueue.dequeue() #person from whom the game restarts
def hotPotato(namelist, num): gamequeue = Queue() for name in namelist: gamequeue.enqueue(name) while gamequeue.size() > 1: for i in range(num): gamequeue.enqueue(gamequeue.dequeue()) gamequeue.dequeue() return gamequeue.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()) aa = simqueue.dequeue() print(aa) return simqueue.dequeue()
def hot_potato(nameslst, num=None): circle = Queue() if not num: num = num_input() print("counter is %d" % (num)) for name in nameslst: circle.enqueue(name) while circle.size() > 1: for i in range(num): circle.enqueue(circle.dequeue()) circle.dequeue() return circle.dequeue()
def hotPotato(namelist, num): simqueue = Queue() for name in namelist: simqueue.enqueue(name) while simqueue.size() > 1: for i in range(1, num): simqueue.enqueue(simqueue.dequeue()) temp = simqueue.dequeue() print(temp + "*" * 10) return simqueue.dequeue()
def __init__(self): # 创建一个队列用来保存进程获取到的数据 self.q = Queue() self.headers = { 'Cookie': 'll="118282"; bid=ctyiEarSLfw; ps=y; __yadk_uid=0Sr85yZ9d4bEeLKhv4w3695OFOPoedzC; dbcl2="155150959:OEu4dds1G1o"; as="https://sec.douban.com/b?r=https%3A%2F%2Fbook.douban.com%2F"; ck=fTrQ; _pk_id.100001.4cf6=c86baf05e448fb8d.1506160776.3.1507290432.1507283501.; _pk_ses.100001.4cf6=*; __utma=30149280.1633528206.1506160772.1507283346.1507290433.3; __utmb=30149280.0.10.1507290433; __utmc=30149280; __utmz=30149280.1506160772.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=223695111.1475767059.1506160772.1507283346.1507290433.3; __utmb=223695111.0.10.1507290433; __utmc=223695111; __utmz=223695111.1506160772.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); push_noty_num=0; push_doumail_num=0', 'Host': 'movie.douban.com', 'Referer': 'https://movie.douban.com/top250?start=225&filter=', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36', }
def try_to_live(live_list, num): queue = Queue() for people in live_list: queue.enqueue(people) while queue.size() > 1: for i in range(num): print(i) queue.enqueue(queue.dequeue()) queue.dequeue() return queue.dequeue()