Beispiel #1
0
def breadth_first(root):
    queue = Queue()
    queue.push(root)

    while not queue.empty():
        node = queue.pop()
        print(node)

        # when you push to a queue, they come to the back, not to the front!
        if not node.leaf():
            queue.push(node.right)
            queue.push(node.left)
def simulation(num_seconds, pages_per_minute):

    lab_printer = Printer(pages_per_minute)
    print_queue = Queue()
    waiting_times = []

    for current_second in range(num_seconds):

        if new_print_task():
            task = Task(current_second)
            print_queue.enqueue(task)
            print(print_queue)

        if (not lab_printer.busy()) and (not print_queue.empty()):
            next_task = print_queue.dequeue()
            waiting_times.append(next_task.wait_time(current_second))
            lab_printer.start_next(next_task)

        lab_printer.tick()
    average_wait = sum(waiting_times) / len(waiting_times)
    print('Average Wait %6.2f secs %3d tasks remaining' %
          (average_wait, print_queue.size()))
Beispiel #3
0
 def _flatten(self, visited_children):
     return Queue([stmt for queue in visited_children for stmt in queue.statements])
Beispiel #4
0
 def visit_block(self, node, visited_children):
     _lbr, *queues, _rbr = visited_children
     return Queue([Block(self._flatten(queues).statements)])
Beispiel #5
0
 def visit_rbracket(self, node, visited_children):
     return Queue([])
Beispiel #6
0
 def visit_comment(self, node, visited_children):
     return Queue([])
Beispiel #7
0
 def visit_whitespace(self, node, visited_children):
     return Queue([])
Beispiel #8
0
 def visit_identifier(self, node, visited_children):
     literal = Identifier(node.text)
     return Queue([literal])
Beispiel #9
0
 def visit_boolean(self, node, visited_children):
     literal = Boolean(node.text == "true")
     return Queue([literal])
Beispiel #10
0
 def visit_number(self, node, visited_children):
     literal = Number(int(node.text))
     return Queue([literal])
Beispiel #11
0
 def visit_string(self, node, visited_children):
     # Drop the wrapping quotes
     contents = node.text[1:-1]
     # Unescape escaped quotes
     literal = String(re.sub(r'\\["bfnrtv\\]', self._unescape_string, contents))
     return Queue([literal])
Beispiel #12
0
 def visit_command(self, node, visited_children):
     assert len(visited_children) == 1
     return Queue(visited_children)
Beispiel #13
0
def breadth_first(root):
    queue = Queue()
    queue.push("separator")
    queue.push(root)

    height = 0
    while not queue.empty():
        node = queue.pop()
        if isinstance(node, Node):
            print(node)
            if not node.leaf():
                queue.push("separator")

                # queue ignores pushes of None, these are potential None pushes
                queue.push(node.left)
                queue.push(node.right)

        if node == "separator":
            print(f"height {height}")
            height += 1
Beispiel #14
0
def hot_potato(players, passes):
    q = Queue()
    for player in players:
        q.enqueue(player)

    while q.size() > 1:
        for i in range(passes):
            has_potato = q.dequeue()
            q.enqueue(has_potato)
        q.dequeue()
    return q.dequeue()