Exemplo n.º 1
0
def hot_potato(namelist, num):
    circle = MyQueue()
    for name in namelist:
        circle.add(name)
    while len(circle) != 1:
        for i in range(num):
            circle.add(circle.pop())
        circle.pop()
    return circle.pop()
Exemplo n.º 2
0
class TestMyQueue(unittest.TestCase):
    def setUp(self):
        # Provide empty queue
        self.queue = MyQueue()

        # Provide filled queue
        self.len_test_data = 5
        self.test_data = [i + 1 for i in range(self.len_test_data)]
        self.filled_queue = MyQueue()
        for i in self.test_data:
            self.filled_queue.push(i)

    def test_instantiation(self):
        queue = MyQueue()
        self.assertIsNotNone(queue)
        self.assertIsNone(queue.latest.top)
        self.assertIsNone(queue.oldest.top)

    def test_len(self):
        # Push and pop repeatedly, still one stack should always be empty
        self.assertEqual(0, len(self.queue))
        self.assertEqual(0, len(self.queue.latest))
        self.assertEqual(0, len(self.queue.oldest))

        self.queue.push(1)
        self.queue.push(1)
        self.assertEqual(2, len(self.queue))
        self.assertEqual(2, len(self.queue.latest))
        self.assertEqual(0, len(self.queue.oldest))

        self.queue.pop()
        self.assertEqual(1, len(self.queue))
        self.assertEqual(0, len(self.queue.latest))
        self.assertEqual(1, len(self.queue.oldest))

    def test_push(self):
        for i in self.test_data:
            self.queue.push(i)
            self.assertEqual(i, len(self.queue))
            self.assertEqual(i, len(self.queue.latest))
            self.assertEqual(0, len(self.queue.oldest))
            self.assertEqual(i, self.queue.latest.top.key)

    def test_pop(self):
        for i in self.test_data:
            data = self.filled_queue.pop()
            self.assertEqual(data, i)
            self.assertEqual(self.len_test_data - i, len(self.filled_queue))
            self.assertEqual(0, len(self.filled_queue.latest))
            self.assertEqual(self.len_test_data - i,
                             len(self.filled_queue.oldest))
            if (i < self.len_test_data):
                self.assertEqual(i + 1, self.filled_queue.oldest.top.key)
Exemplo n.º 3
0
    def list_of_depths(self):
        """
        Create a linked list of all the nodes at each depth.
        For example, a tree with depth D has D linked lists.
        """
        queue = MyQueue()
        the_dict = defaultdict(list)
        level = 0

        queue.push((self, level))
        while (queue.is_empty() == False):
            node, level = queue.pop()
            the_dict[level].append(node.key)
            if (node.left != None):
                queue.push((node.left, level + 1))
            if (node.right != None):
                queue.push((node.right, level + 1))

        return the_dict
Exemplo n.º 4
0
def simulation(total_seconds, pages_per_pages):
    """ 模拟运行环境 """
    printer = Printer(pages_per_pages)
    printer_queue = MyQueue()
    time_per_task = []
    for current_second in range(total_seconds):
        if new_task():
            printer_queue.add(Task(current_second))
        if (len(printer_queue) != 0) and (not printer.isBusy()):
            current_task = printer_queue.pop()
            printer.startTask(current_task)
            time_per_task.append(current_task.waitTime(current_second))
        printer.tick()
    avg_time_per_task = sum(time_per_task) / len(time_per_task)
    print(
        'Average time {:6.2f} secs, total {} tasks, {} tasks remaining'.format(
            avg_time_per_task,
            len(time_per_task),
            len(printer_queue),
        ))
Exemplo n.º 5
0
    def breadth_first_search(graph, start_node, destination_node):
        """
        BFS. Make use of a queue, and make sure to mark nodes as
        visited!
        TODO: Return the path and its cost.
        """
        graph.reset_visited()

        q = MyQueue()
        start_node.visited = True
        q.push(start_node)

        while (q.is_empty() == False):
            node = q.pop()
            if (node == destination_node):
                return True
            for adjacent in node.adjacents:
                q.push(adjacent)

        return False
Exemplo n.º 6
0
def bfs(graph, start):
    """ breadth first search """
    q = MyQueue()
    visited_id = set()  # avoid visit repeated
    v = graph.get_vertex(start)
    if v:
        visited_id.add(v.id)  # add start vertex
        # add v.nbr in queue
        for i in v.get_connections():
            q.add(i)
            visited_id.add(i.id)
    else:
        raise ValueError('Error! start vertex {start} is not in Graph.')
    while q:  # bfs traverse
        current_vert = q.pop()
        print(current_vert.id)  # print vertex id by bfs

        for i in current_vert.get_connections():
            if i.id not in visited_id:
                q.add(i)
                visited_id.add(i.id)