Пример #1
0
def bfs(start: Vertex):
    """Breadth first search which does not effect the edges of the graph"""
    v_queue = Queue()
    v_queue.put(start)
    while not v_queue.empty():
        curr_v = v_queue.get()
        for nbr in curr_v.get_out_neighbors():
            if not nbr.color:
                nbr.color = 'g'
                nbr.add_in_neighbor(curr_v)
                v_queue.put(nbr)
            else:
                pass
                # TODO: delete out_neighbor of curr_v
                # TODO: delete in_neighbor of nbr
        curr_v.color = 'b'
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()))
Пример #4
0
 def test_empty(self):
     q = Queue()
     self.assertTrue(q.empty())
     q.put(1)
     self.assertFalse(q.empty())