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')
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')
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')
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
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)
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 for_each(self, cb): if self.left: self.left.for_each(cb) if self.right: self.right.for_each(cb) return cb(self.value) queue = Queue() queue.enqueue(node)
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(node.value) while queue.__len__() > 0: print(node.value) queue.dequeue() if node.left: node.bft_print(node.left) if node.right: node.bft_print(node.right)
def for_each(self, cb): current_node = self queue = Queue() queue.enqueue(current_node) while queue.len() > 0: current_node = queue.dequeue() cb(current_node.value) for item in [current_node.left, current_node.right]: if item is not None: queue.enqueue(item)
def bft_print(self, node): queue = Queue() curr = node while curr: print(curr.value) if curr.left: queue.enqueue(curr.left) if curr.right: queue.enqueue(curr.right) curr = queue.dequeue()
def bft_print(self, node): storage = Queue() current = self while current: print(current.value) if current.left: storage.enqueue(current.left) if current.right: storage.enqueue(current.right) current = storage.dequeue()
def bft_print(self, node): theQueue = Queue() currentNode = node while currentNode: print(currentNode.value) if currentNode.left: theQueue.enqueue(currentNode.left) if currentNode.right: theQueue.enqueue(currentNode.right) currentNode = theQueue.dequeue()
def bft_print(self, node): current_node = node queue = Queue() queue.enqueue(current_node) while queue.len() > 0: current_node = queue.dequeue() print(current_node.value) for item in [current_node.left, current_node.right]: if item is not None: queue.enqueue(item)
class LRUCache: """ Our LRUCache class keeps track of the max number of nodes it can hold, the current number of nodes it is holding, a doubly- linked list that holds the key-value entries in the correct order, as well as a storage dict that provides fast access to every node stored in the cache. """ def __init__(self, limit=10): self.limit = limit self.cache = Queue() self.dictionary = {} self.length = 0 """ Retrieves the value associated with the given key. Also needs to move the key-value pair to the end of the order such that the pair is considered most-recently used. Returns the value associated with the key or None if the key-value pair doesn't exist in the cache. """ def get(self, key): if key in self.dictionary: node = self.dictionary[key] print(1, node) self.cache.storage.move_to_front(node) # print('aaa',node.value[key]) return node.value[key] else: return None """ Adds the given key-value pair to the cache. The newly- added pair should be considered the most-recently used entry in the cache. If the cache is already at max capacity before this entry is added, then the oldest entry in the cache needs to be removed to make room. Additionally, in the case that the key already exists in the cache, we simply want to overwrite the old value associated with the key with the newly-specified value. """ def set(self, key, value): if key in self.dictionary: node = self.dictionary[key] node.value = {key: value} self.cache.storage.move_to_front(node) else: self.cache.enqueue({ key: value}) self.length += 1 if self.length > self.limit: removed_item = self.cache.dequeue() for dictionary_key in removed_item: del self.dictionary[dictionary_key] self.dictionary[key] = self.cache.storage.head
def bft_print(self, node): storage = Queue() while True: if node == None and storage.len() == 0: break elif node == None: node = storage.dequeue() else: print(node.value) storage.enqueue(node.left) storage.enqueue(node.right) node = storage.dequeue()
def bft_print(self, node): # create a queue q = Queue() # add a root to the queue you create enqueue(node) # while queue is not empty (create a while loop) 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): q = Queue() while node is not None: print(node.value) # Stick all of the node's children in the end of the queue. if node.left: q.enqueue(node.left) if node.right: q.enqueue(node.right) if q.len() > 0: # Get the first node in the queue and continue the loop with it. node = q.dequeue() else: break return
def bft_print(self, node): node_stack = Queue() print(node.value) while node.left or node.right: if node.left: print(node.left.value) node_stack.enqueue(node.left) if node.right: print(node.right.value) node_stack.enqueue(node.right) # Update node node = node_stack.dequeue()
def bft_print(self, node): # create queue queue = Queue() # add root to queue current_node = node # while queue is not empty while current_node: # # DO THE THING!!! (print) print(current_node.value) if current_node.left: # add children of node to queue queue.enqueue(current_node.left) if current_node.right: # add children of node to queue queue.enqueue(current_node.right) # node = pop head of queue current_node = queue.dequeue()
def bft_for_each_i(self, cb): #Create an empty stack q = Queue() # push self onto stack q.enqueue(self) # iterate over the stack while q.len() > 0: # pop the stack off into current node current_node = q.dequeue() # check if node to left if current_node.left: # push the current node's left child onto the stack q.enqueue(current_node.left) # check if node to right if current_node.right: # push the node to the right onto the stack q.enqueue.push(current_node.right) # invoke callback on the value of the current node cb(current_node.value)
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): # Check if this node is not empty if node: # Create a queue my_queue = Queue() # enqueue node my_queue.enqueue(node) # While the length of my_queue is not 0 while my_queue.len() != 0: # save the last node in the queue in popped_node # dequeue this last node from the queue dequeued_node = my_queue.dequeue() # print the value of popped_node print(dequeued_node.value) # If popped_node.left is not empty if dequeued_node.left: # Enqueue popped_node.left to the queue my_queue.enqueue(dequeued_node.left) # If popped_node.right is not empty if dequeued_node.right: # Enqueue popped_node.right to the queue my_queue.enqueue(dequeued_node.right)
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)
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)
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
def bft_print(self, node): # queue = [] # if node: # queue.append(node) # while len(queue) > 0: # print('val', queue[0].value) # current_node = queue[0] # if current_node.left_child: # queue.append(current_node.left_child) # if current_node.right_child: # queue.append(current_node.right_child) # queue.pop(0) queue = Queue() queue.enqueue(node) while queue.len(): current_node = queue.dequeue() print(current_node.value) if current_node.left: queue.enqueue(current_node.left) if current_node.right: queue.enqueue(current_node.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)
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): 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)