def hotPotato(names, num):

    q = Queue()

    for name in names:
        q.enqueue(name)

    while q.size() > 1:
        for x in range(num):
            q.enqueue(q.dequeue())
        q.dequeue()

    return q.dequeue()
Example #2
0
def printerSimulation(numSeconds, ppm: int):
    printer = Printer(ppm)
    printQueue = Queue()
    waitingTimes = []

    for currentSecond in range(numSeconds):
        if newPrintTask():
            task = Task(currentSecond)
            printQueue.enqueue(task)

        if (not printer.isBusy()) and (not printQueue.isEmpty()):
            nextTask = printQueue.dequeue()
            waitingTimes.append(nextTask.waitTime(currentSecond))
            printer.next(nextTask)

        printer.tick()

    averageWaitingTime = sum(waitingTimes) / len(waitingTimes)

    print(
        "Average waiting time is {:.^14.2f} secs and {:.>5d} tasks remaining".
        format(averageWaitingTime, printQueue.size()))