def bfs(start,goal): # dimension of the square puzzle n = len(start) # offsets for movements in the list offsets = { "right": 1, "left": -1, "up": -n, "down": n } # converting matrix to string start_state = "".join(matrix_to_lst(start)) goal_state = "".join(matrix_to_lst(goal)) queue = Queue() queue.enqueue(start_state) predecessors = {start_state: None} while not queue.is_empty(): # pop off first state in the queue current_state = queue.dequeue() # check if it is the goal itself if current_state == goal_state: return get_path(predecessors, start_state, goal_state) current_state_lst = list(current_state) # convert string to list, because swap is not possible on strings(immutable) blank_pos = current_state_lst.index('A') # find position of blank tile for direction in ["up", "right", "down", "left"]: current_copy = copy.deepcopy(current_state_lst) # deepcopy to protect current_state_lst itself from modification new_blank_pos = blank_pos + offsets[direction] if new_blank_pos >= 0 and new_blank_pos < n**2 : # check for valid location before swapping tile current_copy[blank_pos], current_copy[new_blank_pos] = current_copy[new_blank_pos], current_copy[blank_pos] # swap operation neighbour = "".join(current_copy) # convert to string else: continue # if invalid location, continue to next direction if neighbour not in predecessors: queue.enqueue(neighbour) # add neighbour to exploration queue predecessors[neighbour] = current_state # add neighbour to the parent-child dictionary for backtracking return None
def bfs(maze, start, goal): q = Queue() q.enqueue(start) pred = {start: None} while (not q.is_empty()): cc = q.dequeue() if cc == goal: return get_path(pred, start, goal) else: for key, value in offsets.items(): newpos = tuple(cc[i] + value[i] for i in range(len(cc))) if is_legal_pos(maze, newpos) and newpos not in pred: q.enqueue(newpos) pred[newpos] = cc
def bfs(maze, start, goal): queue = Queue() queue.enqueue(start) predecessors = {start: None} while not queue.is_empty(): current_cell = queue.dequeue() if current_cell == goal: return get_path(predecessors, start, goal) for direction in ["up", "right", "down", "left"]: row_offset, col_offset = offsets[direction] neighbor = (current_cell[0] + row_offset, current_cell[1] + col_offset) if is_legal_pos(maze, neighbor) and neighbor not in predecessors: queue.enqueue(neighbor) predecessors[neighbor] = current_cell return None
def __init__(self): self.cats = Queue() self.dogs = Queue()
class AnimalShelter(): """A place for animals to be cared for until they're adopted.""" def __init__(self): self.cats = Queue() self.dogs = Queue() def take_in(self, animal): """Take a pet in until it's adopted.""" # Is there a better way to handle the timing? if type(animal) == Cat: self.cats.enqueue((animal, datetime.datetime.now())) elif type(animal) == Dog: self.dogs.enqueue((animal, datetime.datetime.now())) self.dogs.peek() else: print("We don't take that kind of animal here, sorry!") def introduce_next_pet(self): """Show an interested owner the next pet up for adoption.""" next_cat = self.cats.peek() next_dog = self.dogs.peek() if next_cat[1] < next_dog[1]: return next_cat[0] else: return next_dog[0] def release_animal_to_owner(self, species=None): """Let a person adopt a pet.""" if species is not None: if species.lower() == "cat": return self.cats.dequeue()[0] elif species.lower() == "dog": return self.dogs.dequeue()[0] else: next_cat = self.cats.peek() next_dog = self.dogs.peek() if next_cat[1] < next_dog[1]: return self.cats.dequeue()[0] else: return self.dogs.dequeue()[0]
# Boolean to check if user wishes to continue with app start = True # List of possible selection for category categories = ['G', 'I', 'U', 'P', 'B'] """ Start of App """ print() print("\tWelcome to abcnews Web Scrapper App!\n") # Initialize Queues story_queue = Queue() old_queue = Queue() # Iteration counter i = 0 while start: selection = input( '\tPlease select a category (Type Letter): \nGeneral (G) U.S. (U) International (I) Business (B) Politics (P)\n\nCategory: ' ).upper() print() # Create scrapper object section_headlines = Abcnews_scrapper()