Esempio n. 1
0
    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)
Esempio n. 2
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()
Esempio n. 3
0
def hot_potato(name_list, times):
    name_queue = MyQueue()
    for name in name_list:
        name_queue.enqueue(name)
    while name_queue.size() > 1:
        for i in range(times):
            name_queue.enqueue(name_queue.dequeue())
        name_queue.dequeue()
    return name_queue.dequeue()
Esempio n. 4
0
def bfs(start):
    # start.distance = 0
    # start.previous = None
    vertex_queue = MyQueue()
    vertex_queue.enqueue(start)
    while vertex_queue.size() > 0:
        current = vertex_queue.dequeue()
        for nbr in current.connections:
            if nbr.state == 'unprocessed':
                nbr.state = 'processing'
                nbr.distance = current.distance + 1
                nbr.previous = current
                vertex_queue.enqueue(nbr)
        current.state = 'processed'
Esempio n. 5
0
def simulation(seconds, print_speed):
    printer = Printer(print_speed)
    print_queue = MyQueue()
    wait_time_list = []

    for current_second in range(seconds):
        if Task.new_task():
            task = Task(current_second)
            print_queue.enqueue(task)
        if (not printer.busy()) and (not print_queue.is_empty()):
            new_task = print_queue.dequeue()
            wait_time_list.append(new_task.wait_time(current_second))
            printer.start_next(new_task)
        printer.tick()
    average_wait_time = sum(wait_time_list) / len(wait_time_list)
    print(
        f'Average wait {average_wait_time:6.2f}, {print_queue.size():3d} tasks remaining'
    )
Esempio n. 6
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
Esempio n. 7
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),
        ))
Esempio n. 8
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
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)
Esempio n. 10
0
    def fit(self, training_data, validation_data=None):
        '''
        Override this function to create and destroy workers
        '''

        self._jobs_queue = mp.Queue()
        self._res_queue = MyQueue()
        n_cpus = mp.cpu_count()
        self._processes = [
            Worker(self._jobs_queue, self._res_queue) for i in range(n_cpus)
        ]
        for p in self._processes:
            p.start()

        try:
            super().fit(training_data, validation_data)
        except:
            for p in self._processes:
                p.terminate()
            raise

        for p in self._processes:
            p.terminate()
Esempio n. 11
0
 def test_instantiation(self):
     queue = MyQueue()
     self.assertIsNotNone(queue)
     self.assertIsNone(queue.latest.top)
     self.assertIsNone(queue.oldest.top)
Esempio n. 12
0
from my_queue import MyQueue
from user_interface import *

queues = {"a": MyQueue(), "b": MyQueue(), "c": MyQueue()}

VERY_PRIVATE_PASS = "******"

if __name__ == "__main__":
    while True:
        welcome()
        choice = choose()

        if choice in queues.keys():
            q = queues[choice]
            place = q.add_element()
            number_info(choice, place)
        elif choice == VERY_PRIVATE_PASS:
            official_welcome()
            officials_choice = choose()
            q = queues[officials_choice]
            place = q.remove_element()
            you_are_handling(officials_choice, place)
        else:
            queue_not_found()
Esempio n. 13
0
from my_queue import MyQueue

k1 = MyQueue(4)

k1.add(3)
k1.add(3123)
k1.add(3)
k1.add(123)
k1.add(4)
k1.add(0)
k1.add(2)
k1.add(1)
print(k1.size())

k1.remove()
k1.remove()
k1.remove()
k1.remove()
k1.remove()
k1.remove()
from my_queue import MyQueue
from led import MyPiLed

try:
    # Running code
    led1 = MyPiLed(MyPiLed.YELLOW)
    led2 = MyPiLed(MyPiLed.RED)

    q = MyQueue()
    q.put([led1, 50, 0.1])
    q.put([led2, 10, 0.5])

    q.join()

    q.clear()

except KeyboardInterrupt:
    q.clear()
    led1.off()
    led2.off()
    MyPiLed.reset()
Esempio n. 15
0
 def setUp(self) -> None:
     self.test_stack = Stack()
     self.test_queue = MyQueue()
     self.test_array = []