Пример #1
0
 def test_get(self):
     q = Queue()
     q.put(1)
     i = q.get()
     self.assertEqual(1, i)
     with self.assertRaises(IndexError):
         i = q.get()
Пример #2
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()
Пример #4
0
 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())
Пример #5
0
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()
Пример #6
0
 def test_put(self):
     q = Queue()
     q.put(1)
     self.assertEqual([1], q.items)
Пример #7
0
 def test_size(self):
     q = Queue()
     self.assertEqual(0, q.size())
     q.put(1)
     self.assertEqual(1, q.size())
Пример #8
0
 def test_empty(self):
     q = Queue()
     self.assertTrue(q.empty())
     q.put(1)
     self.assertFalse(q.empty())