Beispiel #1
0
    def find_new_room(self):
        """
        Return a list containing the shortest path from
        starting room to unknown room in
        breadth-first order.
        """

        # # initialize queue with starting vertex
        q = Queue()
        q.enqueue((self.seen[str(self.current_room['room_id'])], []))

        # # set to keep track of vertexes already seen
        visited = set()

        # # while queue is not empty
        while q.len() > 0:
            # get path and vertex
            room, path = q.dequeue()

            # if room contains directions that have not been seen,
            # append direction and return path
            for direction, next_room in room['exits'].items():
                if next_room == '?':
                    path.append(direction)
                    return path
            # else, add room_id to visited
            if room['room_id'] not in visited:
                visited.add(room['room_id'])
                # and add paths to the queue for each exit
                for direction, next_room in room['exits'].items():
                    path_copy = path.copy()
                    path_copy.append(direction)
                    q.enqueue((self.seen[str(next_room)], path_copy))

        raise ValueError('Room not found')
Beispiel #2
0
    def bft_print(self, node):
        #PLAN
        #create/initialize queue
        #add root to queue
        #while queue is not empty
        #pop head/top item out of queue & into temp variable  [node = pop head of queue]
        #if temp exists
        # if there is temp var on the right
        #put that into the queue on the right
        # if there is a left temp var
        #put that into the queue on the left
        # DO the THING!... print temp value
        # else break

        queue = Queue()
        queue.enqueue(node)
        while queue.len() != 0:
            temp: BinarySearchTree = queue.dequeue()
            if temp:
                if temp.right:
                    queue.enqueue(temp.right)
                if temp.left:
                    queue.enqueue(temp.left)
                print(temp.value)

            else:
                break
class RingBuffer:
    def __init__(self, capacity):
        self.capacity = capacity
        self.current = None
        self.storage = DoublyLinkedList()
        self.order = Queue()

    def append(self, item):
        if len(self.storage) == self.capacity:
            node = self.order.dequeue()
            node.value = item
            self.order.enqueue(node)
        else:
            self.storage.add_to_tail(item)
            self.order.enqueue(self.storage.tail)

    def get(self):
        list_buffer_contents = [''] * len(self.storage)
        node = self.storage.head
        i = 0
        while node:
            list_buffer_contents[i] = node.value
            i += 1
            node = node.next
        return list_buffer_contents
Beispiel #4
0
    def bft_print(self, node):
        """
        Iterative approach notes from lecture
        Make a queue
        add root to queue
        while queue.length > 0
        immediately pop root and save to a temp var (temp = queue.dequeue()) - with a queue, popping doesn't have to happen immediately
        Do the thing
        if temp.left add to queue
        if temp.right add to queue
        """
        # instantiating queue and adding initial node
        level_queue = Queue()
        level_queue.enqueue(node)

        # while the queue has stuff in it
        while level_queue.size > 0:
            # removing current node and save it as current node
            current_node = level_queue.dequeue()

            # printing each current node
            print(current_node.value)

            # if there is a node to the right, also add that to queue
            if current_node.right:
                level_queue.enqueue(current_node.right)

            # if there is a node to the right, also add that to queue
            if current_node.left:
                level_queue.enqueue(current_node.left)
    def bft_print(self, node):
        # Initialize a queue
        queue = Queue()

        # Push root to queue
        queue.enqueue(node)

        # While stack not empty
        while queue.len() > 0:

            # Pop item out of queue into temp
            current_node = queue.dequeue()

            # If temp has right, put into queue
            if current_node.right:

                # Ram it through method again
                queue.enqueue(current_node.right)

            # If temp has left, put into queue
            if current_node.left:

                # Ram it through method again
                queue.enqueue(current_node.left)

            # Print
            print(current_node.value)
Beispiel #6
0
    def bft_print(self, node):

        #So to print every value in bredth
        #we use as a queue as we call a node
        #we put the child nodes at the back of the queue
        #this enforces calling all nodes of one level before the next level of nodes are called

        q = Queue()
        #enqueue adds a value to the back of the line.
        #to start the function we put the node passed in at the back/front of the queue
        q.enqueue(node)

        #now, while the queue is not empty...
        while q.len() > 0:
            #we take the node at the front of the queue
            #and set it to current node
            current_node = q.dequeue()
            #print its value
            print(current_node.value)
            #if there is a node to the left, add it to the queu
            if current_node.left:
                q.enqueue(current_node.left)
            #same for right
            if current_node.right:
                q.enqueue(current_node.right)
Beispiel #7
0
    def find_room_by_id(self, id):
        """
        Return a list containing the shortest path from
        starting room to room with matching id in
        breadth-first order.
        """

        # # initialize queue with starting vertex
        q = Queue()
        q.enqueue((self.seen[str(self.current_room['room_id'])], []))

        # # set to keep track of vertexes already seen
        visited = set()

        # # while queue is not empty
        while q.len() > 0:
            # get path and vertex
            room, path = q.dequeue()

            # if room title matches, return path
            if room['room_id'] == id:
                return path

            # else, add room_id to visited
            if room['room_id'] not in visited:
                visited.add(room['room_id'])
                # and add paths to the queue for each exit
                for direction, next_room in room['exits'].items():
                    path_copy = path.copy()
                    path_copy.append(direction)
                    q.enqueue((self.seen[str(next_room)], path_copy))

        raise ValueError('Room not found')
Beispiel #8
0
    def find_nearest_unexplored(self, explored):
        """
        Return a list containing the shortest path from
        starting room to unexplored room in
        breadth-first order.
        """

        # initialize queue with starting vertex
        q = Queue()
        q.enqueue((self.seen[str(self.current_room['room_id'])], []))

        # while queue is not empty
        while q.len() > 0:
            # get path and vertex
            room, path = q.dequeue()

            # if unexplored room, return path
            if room['room_id'] in explored:
                return path

            # else, add room_id to explored
            else:
                explored.add(room['room_id'])
                # and add paths to the queue for each exit
                for direction, next_room in room['exits'].items():
                    path_copy = path.copy()
                    path_copy.append(direction)
                    q.enqueue((self.seen[str(next_room)], path_copy))

        raise ValueError('No more rooms to explore')
Beispiel #9
0
 def bft_print(self, node):
   q = Queue()
   q.enqueue(node)
   while q.len() > 0:
     current = q.dequeue()
     print(current.value)
     current.left and q.enqueue(current.left)
     current.right and q.enqueue(current.right)
Beispiel #10
0
 def bft_print(self, node):
     to_print = Queue()
     to_print.enqueue(node)
     while to_print.len() > 0:
         dequeued_node = to_print.dequeue()
         print(dequeued_node.value)
         dequeued_node.left and to_print.enqueue(dequeued_node.left)
         dequeued_node.right and to_print.enqueue(dequeued_node.right)
 def bft_print(self, node):
     q = Queue()
     q.enqueue(node)
     while q.len() > 0:
         t = q.dequeue()
         print(t.value)
         if t.left:
             q.enqueue(t.left)
         if t.right:
             q.enqueue(t.right)
 def bft_print(self, node):
     storage = Queue()
     storage.enqueue(self)
     while storage.len():
         current_node = storage.dequeue()
         print(current_node.value)
         if current_node.left:
             storage.enqueue(current_node.left)
         if current_node.right:
             storage.enqueue(current_node.right)
 def bft_print(self, node):
     queue = Queue()
     queue.enqueue(node)
     while queue.len() > 0:
         current = queue.dequeue()
         print(current.value)
         if current.left:
             queue.enqueue(current.left)
         if current.right:
             queue.enqueue(current.right)
Beispiel #14
0
 def bft_print(self, node):
     queue = Queue()
     queue.enqueue(node)
     while queue.len() > 0:
         temp = queue.dequeue()
         print(temp.value)
         if temp.left:
             queue.enqueue(temp.left)
         if temp.right:
             queue.enqueue(temp.right)
 def bft_print(self, node=None):
     q = Queue()
     q.enqueue(self)
     while q.len() > 0:
         node_current = q.dequeue()
         print(node_current.value)
         if node_current.left:
             q.enqueue(node_current.left)
         if node_current.right:
             q.enqueue(node_current.right)
 def bft_print(self, node):
     queue = Queue()
     queue.enqueue(node)
     while queue.size > 0:
         n = queue.dequeue()
         print(n.value)
         if n.left:
             queue.enqueue(n.left)
         if n.right:
             queue.enqueue(n.right)
 def bft_print(self, node):
     queue = Queue()
     queue.enqueue(node)
     while queue.size > 0:
         current = queue.dequeue()
         if current.left is not None:
             queue.enqueue(current.left)
         if current.right is not None:
             queue.enqueue(current.right)
         print(current.value)
 def bft_print(self, node):
     q = Queue()
     q.enqueue(node)
     while q.size > 0:
         popped = q.dequeue()
         print(popped.value)
         if popped.left:
             q.enqueue(popped.left)
         if popped.right:
             q.enqueue(popped.right)
 def bft_print(self, node):
     queue = Queue()
     queue.enqueue(node)
     while queue.len() > 0:
         head = queue.dequeue()
         print(head.value)
         if current_node.left is not None:
             queue.enqueue(head.left)
         if current_node.right is not None:
             queue.enqueue(head.right)
 def bft_print(self, node):
     queue = Queue()
     queue.enqueue(node)
     while queue.size > 0:
         curr_node = queue.dequeue()
         print(curr_node.value)
         if curr_node.left:
             queue.enqueue(curr_node.left)
         if curr_node.right:
             queue.enqueue(curr_node.right)
Beispiel #21
0
 def bft_print(self, node):
     queue = Queue()
     queue.enqueue(self)
     while queue.size != 0:
         popped = queue.dequeue()
         print(popped.value)
         if popped.left:
             queue.enqueue(popped.left)
         if popped.right:
             queue.enqueue(popped.right)
 def bft_print(self, node):
     q = Queue()
     q.enqueue(self)
     while q.len() > 0:
         current_node = q.dequeue()
         if current_node.left:
             q.enqueue(current_node.left)
         if current_node.right:
             q.enqueue(current_node.right)
         print(current_node.value)
Beispiel #23
0
 def bft_print(self, node):
     q = Queue()
     q.enqueue(node)
     while q.len() > 0:
         current_pop = q.dequeue()
         print(current_pop.value)
         if current_pop.left:
             q.enqueue(current_pop.left)
         if current_pop.right:
             q.enqueue(current_pop.right)
Beispiel #24
0
 def bft_print(self, node):
     new_queue = Queue()
     new_queue.enqueue(node)
     while new_queue.size > 0:
         current = new_queue.dequeue()
         print(current.value)
         if current.left:
             new_queue.enqueue(current.left)
         if current.right:
             new_queue.enqueue(current.right)
 def bft_print(self, node):
     q = Queue()
     q.enqueue(self)
     while q.len() != 0:
         node = q.dequeue()
         print(node.value)
         if node.left:
             q.enqueue(node.left)
         if node.right:
             q.enqueue(node.right)
Beispiel #26
0
 def bft_print(self, node):
     queue = Queue()
     temp_node = node
     while temp_node:
         print(temp_node.value)
         if temp_node.left:
             queue.enqueue(temp_node.left)
         if temp_node.right:
             queue.enqueue(temp_node.right)
         temp_node = queue.dequeue()
 def bft_print(self, node):
     queue = Queue()
     queue.enqueue(self)
     while queue.len() > 0:
         node = queue.dequeue()
         print(node.value)
         if node.left:
             queue.enqueue(node.left)
         if node.right:
             queue.enqueue(node.right)
 def bft_print(self, node):
     q = Queue()
     q.enqueue(node)
     while q.len() > 0:
         temp = q.dequeue()
         print(temp)
         if temp.left:
             q.enqueue(temp.left)
         if temp.right:
             q.enqueue(temp.right)
 def bft_print(self, node):
     queue = Queue()
     queue.enqueue(node)
     while queue.len() > 0:
         root = queue.dequeue()
         print(root.value)
         if root.left != None:
             queue.enqueue(root.left)
         if root.right != None:
             queue.enqueue(root.right)
Beispiel #30
0
    def bft_print(self, node):
        q = Queue()
        q.enqueue(starting_node)

        while q.len() > 0:
            current = q.dequeue()
            print(current.value)
            if current.left:
                q.enqueue(current.left)
            if current.right:
                q.enqueue(current.right)