def hot_potato(name_lst, num): q = Queue() for n in name_lst: q.enqueue(n) while q.size() > 1: for i in range(num): q.enqueue(q.dequeue()) q.dequeue() return q.dequeue()
class Stack: def __init__(self): self.__stack = Queue() # Adds value to the end of the Stack. # Complexity: O(1) # push(value) def push(self, value): self.__stack.push(value) # Returns value from the end of the Stack and removes it. # Complexity: O(1) # pop() def pop(self, pop_type=None): new_stack = Queue() while self.__stack.size() > 0: current_value = self.__stack.pop() if self.__stack.size() == 0: if pop_type is not None: new_stack.push(current_value) self.__stack = new_stack return current_value new_stack.push(current_value) # Returns value from the end of the Stack without removing it. # Complexity: O(1) def peek(self): return self.pop(1) # Returns the number of elements in the Queue. # Complexity: O(1) def size(self): return self.__stack.size() def getStack(self): return self.__stack.getQueue()
def find_nearest_shop(self): visited = [] queue = Queue() queue.push(self.__start_index) while queue.size() != 0: current = queue.pop() if current is not None: if self.__shop_lists[current] != 0: return current for e in range(len(self.__map[current])): if self.__map[current][e] != 0 and e not in visited: queue.push(e) visited.append(current) return None
def simulate(num_seconds, pages_per_minute): printer = Printer(pages_per_minute) print_queue = Queue() waiting_times = [] for curr_sec in range(num_seconds): if is_create_new_print_task(): task = RandomTask(curr_sec) print_queue.put(task) if (not printer.is_busy()) and (not print_queue.empty()): next_task = print_queue.get() waiting_times.append(next_task.get_wait_time(curr_sec)) printer.start_next(next_task) printer.tick() avg_wait_time = sum(waiting_times) / len(waiting_times) return avg_wait_time, print_queue.size()
def simulation(numSeconds, pagesPerMinute): 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.empty()): 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 print_animation(self, names: list, num: int): circle_q = Queue() for name in names: circle_q.put(name) while True: # Stop the process when only one name remains if circle_q.size() == 1: break # When passing a potato, the simulation will simply dequeue and then immediately # enqueue the child, putting her at the end of the line for i in range(num): front_child = circle_q.get() circle_q.put(front_child) print("Time:", i) print(self.circle_q_to_str(names, front_child)) time.sleep(1) # After passing potatoes `num` times, the child at the front will be removed permanently name = circle_q.get() names.remove(name) print('\n') print("The last man:", circle_q.get())
def get_last(names: list, num: int) -> str: """ :param names: a list of names :param num: the time of each time's passing :return: the name of the last person remaining after repetitive counting by `num` """ # Use a queue to simulate the circle # Child holding the potato will be at the front of the queue circle_q = Queue() for name in names: circle_q.put(name) while True: # Stop the process when only one name remains if circle_q.size() == 1: break # When passing a potato, the simulation will simply dequeue and then immediately # enqueue the child, putting her at the end of the line for _ in range(num): front_child = circle_q.get() circle_q.put(front_child) # After passing potatoes `num` times, the child at the front will be removed permanently circle_q.get() return circle_q.get()
def test_size(self): q = Queue() self.assertEqual(0, q.size()) q.put(1) self.assertEqual(1, q.size())