def bfs(self): self.clear() self._display_grid() # frontier is where we've yet to go frontier: Queue[Node[T]] = Queue() frontier.push(Node(self.start, None)) # explored is where we've been explored: Set[T] = {self.start} self.step(frontier, explored, None)
class TestQueue(TestStack): # inherit from testStack since similar tests will be run with Queue def setUp(self): self.structure = Queue() def test_structure_peek(self): structure = self.add_to_structure( self, iter(['people', 'potatoes', 'pansies'])) # first word to the queue appears with peak self.assertEqual(self.structure.peek(), 'people') def test_structure_get(self): structure = self.add_to_structure(self, range(3)) # test that last in are first out for i in range(3): self.assertEqual(self.structure.get(), i)
def test_push(): queue = Queue() assert (queue.is_empty()) for i in range(ITERS): queue.push(i) assert (len(queue) == i + 1) assert (queue.peek() == 0) assert (not queue.is_empty())
def test_peek(self): q = Queue(1) q.enqueue(2) q.enqueue(5) self.assertEqual(q.peek(), 1) q = Queue() # empty queue self.assertRaises(IndexError, q.peek) # supposed to raise IndexError
def test_enqueue(self): q = Queue() q.enqueue(1) q.enqueue(2) q.enqueue(3) self.assertEqual(len(q), 3)
def bfs(self, starting_room, target_room): """ Return a list containing the shortest path from starting_room to destination_room, after searching for and finding it with a breadth-first search (BFS) algorithm. """ # Initialize empty queue and set of BFS-"visited" rooms: queue = Queue() visited_bfs = set() # Initialize path (we will add the rest of the path from starting room to target room below): path_rooms = [starting_room] path_directions = [] # Check if provided starting room is in our map: if starting_room in self.rooms.keys(): # If so, add starting room to queue: queue.enqueue((path_rooms, path_directions)) else: return IndexError( f"Provided starting room {starting_room} does not exist in map!" ) # Search all rooms via BFS, logging their paths (from starting_room --> room) as we go: while queue.size() > 0: # Get next room to process as first item in queue: current_path_rooms, current_path_directions = queue.dequeue() current_room = current_path_rooms[-1] # Check if it is the target --> if so, return its full path: if current_room == target_room: return current_path_directions # Alternative: return (current_path_rooms, current_path_directions) # If the current room has not already been visited+processed, check and process it: if current_room not in visited_bfs and current_room in self.rooms: # If not, then get its neighbor rooms and add their paths to the queue for future processing:s neighbors = self.get_neighbors(current_room) for adjacent_room_direction in neighbors: adjacent_room_path_rooms = current_path_rooms + [ neighbors[adjacent_room_direction] ] adjacent_room_path_directions = current_path_directions + [ adjacent_room_direction ] queue.enqueue((adjacent_room_path_rooms, adjacent_room_path_directions)) # Mark current room as "visited" by adding to our set of visited rooms: visited_bfs.add(current_room) # If no path found in entire map, return None: return None
def setUp(self): self.grid = np.genfromtxt("data_np.txt", delimiter=",", dtype=np.int) self.start = (4, 0) self.end = (0, 4) self.dfs_correct_returns = { "visited_list": [(4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (3, 4), (3, 3), (3, 2), (2, 3), (2, 4), (1, 4), (0, 4)], "path_list": [(0, 4), (1, 4), (2, 4), (2, 3), (3, 3), (3, 4), (4, 4), (4, 3), (4, 2), (4, 1), (4, 0)] } self.bfs_correct_returns = { "visited_list": [(4, 0), (3, 0), (4, 1), (4, 2), (3, 2), (4, 3), (3, 3), (4, 4), (2, 3), (3, 4), (2, 4), (1, 4), (0, 4)], "path_list": [(0, 4), (1, 4), (2, 4), (2, 3), (3, 3), (3, 2), (4, 2), (4, 1), (4, 0)] } self.a_star_correct_returns = { "visited_list": [(4, 0), (3, 0), (4, 1), (3, 2), (2, 3), (1, 4), (0, 4)], "path_list": [(0, 4), (1, 4), (2, 3), (3, 2), (4, 1), (4, 0)] } self.test_nodes = [ Node(self.grid, (4, 0), cost=0, heuristic=12), Node(self.grid, (3, 0), cost=10, heuristic=10), Node(self.grid, (4, 1), cost=10, heuristic=8) ] self.maze = Maze(self.grid) self.priority_queue = PriorityQueue() self.stack = Stack() self.queue = Queue() self.visited = VisitedNodes()
def test_stacks_to_queues(): print('{}'.format('-' * 20)) print('Start of Task 1 Testing (stacks_to_queues):\n') cases = [[1, 2, 3], [], [10, 20], [100, 200, 300, 400]] sizes = [3, 1, 2, 4] master_stack1 = Stack(len(cases)) for i in range(len(cases) - 1, -1, -1): stack = Stack(sizes[i]) for item in cases[i]: stack.push(item) master_stack1.push(stack) print('master_stack before calling function:') print(master_stack1) print() print('master_queue1 after calling function:') master_queue1 = stacks_to_queues(master_stack1) print(master_queue1) print() print('Verity that master_stack1 is empty after calling function:') print(master_stack1) print() print('Verifying Sizes:') counter = 0 while not master_queue1.is_empty(): print('Queue {} size = {}'.format(counter, master_queue1.remove()._size)) counter += 1 print() master_queue2 = stacks_to_queues(Stack(5)) print('Contents of master_queue if master_stack is empty: {}'.format( master_queue2)) print('Size of master_queue2 = {}'.format(master_queue2._size)) print() master_queue3 = stacks_to_queues(Queue(5)) print('Contents of master_queue if master_stack is invalid: {}'.format( master_queue3)) print('Size of master_queue3 = {}'.format(master_queue2._size)) print() print('End of Task 1 testing') print('{}\n'.format('-' * 20)) return
def bfs(graph: Graph, start_node: str, target_node: str, visited_nodes: set): queue = Queue() queue.enqueue(start_node) while not queue.is_empty(): current = queue.deque() if current == target_node: return True adj = graph.get_edges_node(current) for node in adj: if node not in visited_nodes: queue.enqueue(node) visited_nodes.add(current) return False
def test_queue_size_after_dequeue_on_complete_queue(self): queue = Queue() items = [3, 4, 1, 111, "abcd", "item 5"] for item in items: queue.enqueue(item) # Dequeue operation on complete queue for i in range(len(items)): queue.dequeue() self.assertEqual(queue.length, QUEUE_LENGTH_ZERO)
def bfs(self, starting_vertex, target_vertex): """ Return a list containing the shortest path from starting_vertex to destination_vertex, after searching for and finding it with a breadth-first search (BFS) algorithm. """ # Initialize empty queue and set of visited nodes: queue = Queue() visited = set() # Initialize path (we will add the rest of the path from starting vertex to target vertex below): path = [starting_vertex] # Check if provided starting vertex is in our graph: if starting_vertex in self.vertices.keys(): # If so, add starting vertex to queue: queue.enqueue(path) else: return IndexError( f"Provided starting vertex {starting_vertex} does not exist in graph!" ) # Process all vertices via BFT: while queue.size() > 0: # Get next vertex to process as first item in queue: current_path = queue.dequeue() current_vertex = current_path[-1] # If the current vertex has not already been visited+processed, check and process it: if current_vertex not in visited: # Check if it is the target --> if so, return its full path: if current_vertex == target_vertex: return current_path # If not, then get its neighbor vertices and add their paths to the queue for future processing: for adjacent_vertex in self.get_neighbors(current_vertex): adjacent_vertex_path = current_path + [adjacent_vertex] queue.enqueue(adjacent_vertex_path) # Mark current vertex as "visited" by adding to our set of visited vertices: visited.add(current_vertex) # If no path found in entire graph, return None: return None
def test_queue(): f = Queue() test_array = [i for i in range(100)] for i in test_array: f.enqueue(i) result = [] while not f.is_empty(): result.append(f.deque()) assert test_array == result
def bfs(grid:np.array,start:tuple,end:tuple) -> Tuple[list,list]: """ A function that searches for the shortest path in a grid using the Breadth-first-search algorithm This function searches through a grid searching for the shortest path using the Breadth-first-search algorithm. First a Queue (queue) object is created and the first node is pushed into it. A VisitedNodes (visited) object is created after. While there are objects in queue, the first object in the queue is removed and placed in visited. If the node's position is equal to the end, then a visited list and a path list is returned. Otherwise, each child of the node is iterated upon and checked to see if they are in visited or the queue, and if that child is accessible. If true, then that child node is pushed into the queue and the process repeats. Parameters ---------- grid : np.array a numpy array detailing the grid start : tuple a tuple detailing the starting node's position end : tuple a tuple detailing the ending node's position Returns ------- Tuple[list,list] A list of visited nodes and a list of nodes that are included in the path """ if grid[start[0]][start[1]] == 1 or grid[end[0]][end[1]] == 1: return [None],[None] queue = Queue() queue.push(Node(grid,start)) visited= VisitedNodes() while len(queue) > 0: node = queue.pop() visited._store_node(node) if node.pos == end: visited_list, path_list = visited.create_path(node.pos, start) return visited_list, path_list else: for child in node.children: if child["node"] not in visited.visited_nodes and child["node"] not in queue.queue_list and child["accessibility"]: queue.push(Node(grid, child["node"], parent=node.pos)) return [None],[None]
def bfs(initial, goal_test, successors): """ Breadth-first search. Parameters ---------- initial : Generic Starting point of the search. goal_test : Callable Callable returing boolean value indicating search success. successors : Callable Callable returning list of next possible locations in search space. Returns ------- found : Generic Node corresponding to successful goal_test. Returns None if search fails. """ # References to candidate and previously-explored nodes in search space frontier = Queue() explored = Stack() # Initialize candidate search locations with initial condition frontier.push(Node(initial, None)) # Continue search as long as their are candidates in the search space while not frontier.empty: current_node = frontier.pop() current_state = current_node.state # If current node meets goal, then search completes successfully if goal_test(current_state): return current_node # Populate next step in search for child in successors(current_state): # Skip previously-explored states if child in explored: continue explored.push(child) frontier.push(Node(child, current_node)) # Search terminates without finding goal return None
def simulation(num_seconds, pages_per_minute, num_students): lab_printer = Printer(pages_per_minute) print_queue = Queue() waiting_times = [] for current_second in range(num_seconds): if new_print_task(num_students): task = Task(current_second) print_queue.enqueue(task) if (not lab_printer.busy()) and (not print_queue.is_empty()): nexttask = print_queue.dequeue() waiting_times.append(nexttask.wait_time(current_second)) lab_printer.start_next(nexttask) lab_printer.tick() average_wait = sum(waiting_times) / len(waiting_times) print("Average Wait %6.2f secs %3d tasks remaining." % (average_wait, print_queue.size()))
def simulation(seconds, ppm): printer = Printer(ppm) print_queue = Queue() waiting_times = [] for sec in range(seconds): if is_new_task(): task = Task(sec) print_queue.enqueue(task) if not printer.busy() and not print_queue.is_empty(): next_task = print_queue.dequeue() waiting_times.append(next_task.wait_time(sec)) printer.start_next(next_task) printer.tick() average_wait = sum(waiting_times) / len(waiting_times) print("Average Wait %6.2f secs %3d tasks remaining." % (average_wait, print_queue.size()))
def simulation(numSeconds, numStudents, pagesPerMinute): labprinter = Printer(pagesPerMinute) printQueue = Queue() waitingtimes = [] for currentSecond in range(numSeconds): if newPrintTask(numStudents): task = Task(currentSecond) printQueue.enqueue(task) if (not labprinter.busy()) and (not printQueue.isEmpty()): nexttask = printQueue.dequeue() waitingtimes.append(nexttask.waitTime(currentSecond)) labprinter.startNext(nexttask) labprinter.tick() averageWait = sum(waitingtimes) / len(waitingtimes) print("Average Wait %6.2f secs %3d tasks remaining." % (averageWait, printQueue.size())) return averageWait
def setUp(self): self.emptyQueue = Queue() self.Queue = Queue() for i in range(5): self.Queue.enqueue(i)
class TestQueue(unittest.TestCase): # FIFO def setUp(self): self.emptyQueue = Queue() self.Queue = Queue() for i in range(5): self.Queue.enqueue(i) def test_repr(self): '''Test returning the queue as a string literal.''' self.assertEqual(repr(self.emptyQueue), str(())) tup = (1, 3.14, 'foo', True) for i in tup: self.emptyQueue.enqueue(i) self.assertEqual(repr(self.emptyQueue), str(tup)) def test_len_size(self): '''Test returning the size of the queue.''' for i in range(100): self.emptyQueue.enqueue(i) self.assertEqual(len(self.emptyQueue), 100) self.assertEqual(self.emptyQueue.size(), 100) self.emptyQueue.dequeue() self.assertEqual(len(self.emptyQueue), 99) self.assertEqual(self.emptyQueue.size(), 99) def test_tuple(self): '''Test returning the queue as a tuple literal.''' self.assertEqual(tuple(self.emptyQueue), ()) tup = (1, 3.14, 'foo', True) for i in tup: self.emptyQueue.enqueue(i) self.assertEqual(tuple(self.emptyQueue), tup) def test_list(self): '''Test returning the queue as a list literal.''' self.assertEqual(list(self.emptyQueue), []) li = [1, 3.14, 'foo', True] for i in li: self.emptyQueue.enqueue(i) self.assertEqual(list(self.emptyQueue), li) def test_enqueue(self): '''Test adding items to the front of the queue.''' self.Queue.enqueue(True) self.assertEqual(tuple(self.Queue), (0, 1, 2, 3, 4, True)) def test_dequeue(self): '''Test removing items from the front of the queue.''' self.assertEqual(self.Queue.dequeue(), 0) self.assertEqual(tuple(self.Queue), (1, 2, 3, 4)) self.assertEqual(self.Queue.dequeue(), 1) self.assertEqual(self.Queue.dequeue(), 2) self.assertEqual(self.Queue.dequeue(), 3) self.assertEqual(self.Queue.dequeue(), 4) self.assertEqual(tuple(self.Queue), ()) with self.assertRaises(ValueError): self.Queue.dequeue() # test enqueuing after dequeuing self.Queue.enqueue(0) self.Queue.enqueue(True) self.assertEqual(tuple(self.Queue), (0, True)) def test_peek(self): '''Test peeking at the first enqueued item w/o modifying the queue.''' self.assertEqual(self.Queue.peek(), 0) with self.assertRaises(ValueError): self.emptyQueue.peek()
from data_structures import Stack, Queue, Deque stack = Stack() queue = Queue() deque = Deque() print("*** Stack ***") print(stack) for i in range(5): stack.push(i) print(stack) for i in range(stack.size()): stack.pop() print(stack) print("*** Queue ***") print(queue) for i in range(5): queue.enqueue(i) print(queue) for i in range(queue.size()): queue.dequeue() print(queue) print("*** Deque - Stack operation ***") print(deque) for i in range(5): deque.add_rear(i)
def setUp(self): self.structure = Queue()
def setUp(self): self.stack = Stack(range(1, 6)) self.queue = Queue(range(1, 6)) self.single = SinglyLinkedList() self.double = DoublyLinkedList() self.btree = BinaryTree()
def interleave_stack(stack): ''' This problem was asked by Google. Given a stack of N elements, interleave the first half of the stack with the second half reversed using only one other queue. This should be done in-place. Recall that you can only push or pop from a stack, and enqueue or dequeue from a queue. For example, if the stack is [1, 2, 3, 4, 5], it should become [1, 5, 2, 4, 3]. If the stack is [1, 2, 3, 4], it should become [1, 4, 2, 3]. ''' stack_size = 0 queue = Queue() while True: try: queue.enqueue(stack.pop()) stack_size += 1 except IndexError: break while True: try: stack.push(queue.dequeue()) except IndexError: break while True: try: queue.enqueue(stack.pop()) except IndexError: break while True: try: stack.push(queue.dequeue()) except IndexError: break for regurgitation_size in range(stack_size - 1, 0, -1): for _ in range(regurgitation_size): queue.enqueue(stack.pop()) for _ in range(regurgitation_size): stack.push(queue.dequeue()) return stack
def test_blank_queue_object(self): q = Queue() self.assertEqual(len(q), 0)
def test_pop(): queue, arr = Queue(), [] assert (queue.is_empty()) with pytest.raises(AssertionError): queue.pop() for _ in range(ITERS): value = random.randrange(MAX_VAL) queue.push(value) arr.append(value) assert (len(queue) == len(arr)) for _ in range(ITERS): assert (not queue.is_empty()) expected, arr = arr[0], arr[1:] actual = queue.peek() assert (actual == expected) actual = queue.pop() assert (actual == expected) assert (len(queue) == len(arr)) assert (queue.is_empty()) with pytest.raises(AssertionError): queue.pop()
def single_bfs(big_list, current): queue = Queue() explored = Queue() newExplored = Stack() seen = set() pointer = current queue.enqueue(pointer) #Add the our initial position to the explored.enqueue(pointer) #queue tra_model = Tra_model() while True: if queue.isEmpty(): break else: pointer = queue.dequeue() # First move to the right and check what it is pointer = tra_model.move_right(pointer) #make the first move x = pointer[0] y = pointer[1] if big_list[x][y] == " ": #if it is space, add its location queue.enqueue(pointer) explored.enqueue(pointer) big_list[x][y] = "#" pointer = tra_model.move_left(pointer) if big_list[x][y] == "%" or "#": #if it is hardle, move back pointer = tra_model.move_left(pointer) if big_list[x][y] == ".": #if it is goal, add its location queue.enqueue(pointer) explored.enqueue(pointer) break # second move to the bottom and check what it is pointer = tra_model.move_down(pointer) x = pointer[0] y = pointer[1] if big_list[x][y] == " ": #if it is space, add its location explored.enqueue(pointer) queue.enqueue(pointer) big_list[x][y] = "#" pointer = tra_model.move_up(pointer) if big_list[x][y] == "%" or "#": #if it is hardle, move back pointer = tra_model.move_up(pointer) if big_list[x][y] == ".": #if it is goal, add its location queue.enqueue(pointer) explored.enqueue(pointer) break # Third move to the left and check what it is pointer = tra_model.move_left(pointer) x = pointer[0] y = pointer[1] if big_list[x][y] == " ": #if it is space, add its location explored.enqueue(pointer) queue.enqueue(pointer) big_list[x][y] = "#" pointer = tra_model.move_right(pointer) if big_list[x][y] == "%" or "#": #if it is hardle, move back pointer = tra_model.move_right(pointer) if big_list[x][y] == ".": #if it is goal, add its location queue.enqueue(pointer) explored.enqueue(pointer) break # Fourth move to the left and check what it is pointer = tra_model.move_up(pointer) x = pointer[0] y = pointer[1] if big_list[x][y] == " ": #if it is space, add its location explored.enqueue(pointer) queue.enqueue(pointer) big_list[x][y] = "#" pointer = tra_model.move_down(pointer) if big_list[x][y] == "%" or "#": #if it is hardle, move back pointer = tra_model.move_down(pointer) if big_list[x][y] == ".": #if it is goal, add its location queue.enqueue(pointer) explored.enqueue(pointer) break expanded = 0 for i in explored.items: expanded += 1 steps = 0 for item in explored.items: t = tuple(item) if t not in seen: steps += 1 newExplored.push(item) seen.add(t) return newExplored, big_list, steps, expanded
def __repr__(self): level = 0 q = Queue() visit_order = list() node = self.get_root() q.enqueue( (node,level) ) while q.size() > 0: node, level = q.dequeue() if node == None: visit_order.append( ("<empty>", level)) continue visit_order.append( (node, level) ) if node.has_left_child(): q.enqueue( (node.get_left_child(), level +1 )) else: q.enqueue( (None, level +1) ) if node.has_right_child(): q.enqueue( (node.get_right_child(), level +1 )) else: q.enqueue( (None, level +1) ) s = "Tree\n" previous_level = -1 for i in range(len(visit_order)): node, level = visit_order[i] if level == previous_level: s += " | " + str(node) else: s += "\n" + str(node) previous_level = level return s
def test_queue_object(self): q = Queue(1) self.assertEqual(len(q), 1)
class TestDataStructures(unittest.TestCase): def setUp(self): self.stack = Stack(range(1, 6)) self.queue = Queue(range(1, 6)) self.single = SinglyLinkedList() self.double = DoublyLinkedList() self.btree = BinaryTree() # STACK def test_stack_push(self): self.stack.push(6) self.assertEqual(self.stack.stack, range(1, 7)) def test_stack_pop(self): self.assertEqual(self.stack.pop(), 5) self.assertEqual(self.stack.pop(), 4) def test_stack_peek(self): self.assertEqual(self.stack.peek(), 5) # QUEUE def test_queue_push(self): self.queue.push(6) self.assertEqual(self.queue.queue, range(1, 7)) def test_queue_pop(self): self.assertEqual(self.queue.pop(), 1) self.assertEqual(self.queue.pop(), 2) def test_queue_peek(self): self.assertEqual(self.queue.peek(), 1) self.queue.pop() self.assertEqual(self.queue.peek(), 2) # SINGLY-LINKED LIST def test_add_nodes_to_single(self): node1, node2, node3 = Node(1), Node(2), Node(3) self.single._insert(node1) self.single._insert(node2, node1) self.assertEqual(self.single.head.value, 1) next = self.single.head.next self.assertEqual(next, node2) self.assertEqual(next.value, 2) self.single._insert(node3, node2) nextnext = self.single.head.next.next self.assertEqual(nextnext, node3) def test_add_node_to_beginning_of_single(self): node0, node1 = Node(0), Node(1) self.single._insert(node1) self.single._insert(node0) self.assertEqual(self.single.head.value, 0) self.assertEqual(self.single.head.next.value, 1) def test_remove_nodes_single(self): node1, node2, node3, node4 = Node(1), Node(2), Node(3), Node(4) self.single._insert(node1) self.single._insert(node2, node1) self.single._insert(node3, node2) self.single._insert(node4, node3) self.single._remove(node2) self.assertEqual(node2.next, node4) def test_remove_node_from_beginning_single(self): node1, node2, node3 = Node(1), Node(2), Node(3) self.single._insert(node1) self.single._insert(node2, node1) self.single._insert(node3, node2) self.single._remove() self.assertEqual(self.single.head, node2) def test_single_iteration(self): node1, node2, node3, node4 = Node(1), Node(2), Node(3), Node(4) self.single._insert(node1) self.single._insert(node2, node1) self.single._insert(node3, node2) self.single._insert(node4, node3) self.assertEqual([node for node in self.single], [node1, node2, node3, node4]) def test_add_node_to_single_for_real(self): self.single.insert(2, 0) self.assertEqual(self.single[0].value, 2) self.single.insert(1, 0) self.assertEqual(self.single[0].value, 1) self.single.insert(3, 2) self.assertEqual(self.single[2].value, 3) self.single.insert(4, 100) self.assertEqual(self.single[3].value, 4) def test_remove_node_from_single_for_real(self): for i in range(4, 0, -1): self.single.insert(i, 0) # [1, 2, 3, 4] self.single.remove(1) # [1, 3, 4] self.assertEqual(self.single[1].value, 3) self.single.remove(2) # [1, 3] self.assertEqual(self.single[0].value, 1) self.assertEqual(self.single[1].value, 3) # DOUBLE-LINKED LIST def test_iteration_double(self): node1, node2, node3 = Node(1), Node(2), Node(3) node1.next, node2.next, node3.next = node2, node3, None node1.prev, node2.prev, node3.prev = None, node1, node2 self.double.head = node1 self.double.tail = node3 # test __iter__ self.assertEqual([str(i) for i in self.double], [str(i) for i in range(1, 4)]) # test iterating over reversed self.assertEqual([str(i) for i in reversed(self.double)], [str(i) for i in range(3, 0, -1)]) def test_double_slicing(self): node1, node2, node3 = Node(1), Node(2), Node(3) node1.next, node2.next, node3.next = node2, node3, None node1.prev, node2.prev, node3.prev = None, node1, node2 self.double.head = node1 self.double.tail = node3 self.assertEqual(self.double[0].value, 1) self.assertEqual(self.double[1].value, 2) self.assertEqual(self.double[2].value, 3) self.assertRaises(IndexError, lambda val: self.double[val], 3) def test_base_insert_moving_forwards_with_double(self): node1, node2, node3 = Node(1), Node(2), Node(3) self.double._insert(node1) self.assertTrue(test_node(self.double[0], 1, None, None)) self.assertTrue(test_linked_list(self.double, self.double[0], self.double[0])) self.double._insert(node3, node1) self.assertTrue(test_node(self.double[0], 1, 3, None)) self.assertTrue(test_node(self.double[1], 3, None, 1)) self.assertTrue(test_linked_list(self.double, self.double[0], self.double[1])) self.double._insert(node2, node1) self.assertTrue(test_node(self.double[0], 1, 2, None)) self.assertTrue(test_node(self.double[1], 2, 3, 1)) self.assertTrue(test_node(self.double[2], 3, None, 2)) self.assertTrue(test_linked_list(self.double, self.double[0], self.double[2])) self.assertEqual(str(self.double), str([str(i) for i in range(1, 4)])) def test_base_insert_moving_backwards_with_double(self): node1, node2, node3 = Node(1), Node(2), Node(3) # insert node3 at the beginning/end of the list self.double._rev_insert(node3) self.assertTrue(test_node(self.double[0], 3, None, None)) self.assertTrue(test_linked_list(self.double, self.double[0], self.double[0])) # insert node one before node 3 ([node1, node3]) self.double._rev_insert(node1, node3) self.assertTrue(test_node(self.double[0], 1, 3, None)) self.assertTrue(test_node(self.double[1], 3, None, 1)) self.assertTrue(test_linked_list(self.double, self.double[0], self.double[1])) # insert node2 before node3 ([node1, node2, node3]) self.double._rev_insert(node2, node3) self.assertTrue(test_node(self.double[0], 1, 2, None)) self.assertTrue(test_node(self.double[1], 2, 3, 1)) self.assertTrue(test_node(self.double[2], 3, None, 2)) self.assertTrue(test_linked_list(self.double, self.double[0], self.double[2])) # check that the array is [node1, node2, node3] self.assertEqual(str(self.double), str([str(i) for i in range(1, 4)])) def test_insert_at_beginning_of_double(self): self.double.insert(2, 0) self.assertTrue(test_node(self.double[0], 2, None, None)) self.assertTrue(test_linked_list(self.double, self.double[0], self.double[0])) self.double.insert(1, 0) self.assertTrue(test_node(self.double[0], 1, 2, None)) self.assertTrue(test_node(self.double[1], 2, None, 1)) self.assertTrue(test_linked_list(self.double, self.double[0], self.double[1])) def test_insert_in_middle_and_end_of_double(self): self.double.insert(1, 0) self.double.insert(3, 1) self.assertTrue(test_node(self.double[0], 1, 3, None)) self.assertTrue(test_node(self.double[1], 3, None, 1)) self.assertTrue(test_linked_list(self.double, self.double[0], self.double[1])) self.double.insert(2, 1) self.assertTrue(test_node(self.double[0], 1, 2, None)) self.assertTrue(test_node(self.double[1], 2, 3, 1)) self.assertTrue(test_node(self.double[2], 3, None, 2)) self.assertTrue(test_linked_list(self.double, self.double[0], self.double[2])) self.double.insert(5, 3) self.assertTrue(test_node(self.double[0], 1, 2, None)) self.assertTrue(test_node(self.double[1], 2, 3, 1)) self.assertTrue(test_node(self.double[2], 3, 5, 2)) self.assertTrue(test_node(self.double[3], 5, None, 3)) self.assertTrue(test_linked_list(self.double, self.double[0], self.double[3])) self.double.insert(4, 3) self.assertTrue(test_node(self.double[0], 1, 2, None)) self.assertTrue(test_node(self.double[1], 2, 3, 1)) self.assertTrue(test_node(self.double[2], 3, 4, 2)) self.assertTrue(test_node(self.double[3], 4, 5, 3)) self.assertTrue(test_node(self.double[4], 5, None, 4)) self.assertTrue(test_linked_list(self.double, self.double[0], self.double[4])) # Binary Tree def test_binary_node_equality(self): one = BinaryNode(1) one2 = BinaryNode(1) self.assertTrue(one == one2) two = BinaryNode(2) self.assertFalse(one == two) def test_add_internal_child_to_binary_node(self): one = BinaryNode(1) two = BinaryNode(2) three = BinaryNode(3) one.left = three # one = {left: 3, right: None} one.insert(two, three) self.assertTrue(test_binary_node(one, 1, 2, None, None)) self.assertTrue(test_binary_node(two, 2, 3, None, 1)) self.assertTrue(test_binary_node(three, 3, None, None, 2)) def test_add_external_child_to_binary_node(self): one = BinaryNode(1) two = BinaryNode(2) one.insert(two) self.assertTrue(test_binary_node(one, 1, 2, None, None)) self.assertTrue(test_binary_node(two, 2, None, None, 1)) three = BinaryNode(3) one.insert(three) self.assertTrue(test_binary_node(one, 1, 2, 3, None)) self.assertTrue(test_binary_node(two, 2, None, None, 1)) self.assertTrue(test_binary_node(three, 3, None, None, 1)) four = BinaryNode(4) two.insert(four) self.assertTrue(test_binary_node(two, 2, 4, None, 1)) self.assertTrue(test_binary_node(four, 4, None, None, 2)) def test_depth_first_generation(self): self.btree.root = deep_btree() self.assertEquals([int(str(n)) for n in self.btree.depth_gen(self.btree.root)], range(1, 9)) def test_breadth_first_generation(self): self.btree.root = wide_btree() self.assertEqual([int(str(n)) for n in self.btree.breadth_gen([self.btree.root])], range(1, 9)) def test_binary_tree_iteration(self): self.btree.root = deep_btree() self.assertEquals([int(str(n)) for n in self.btree], range(1, 9)) def test_contains(self): self.btree.root = deep_btree() for i in range(1, 9): self.assertTrue(i in self.btree) self.assertFalse(0 in self.btree) self.assertFalse(9 in self.btree) def test_insert_into_binary_tree(self): ## note - indexing grabs the node with that VALUE, not the node that exists at that index # 1 self.btree.insert(1) self.assertTrue(test_binary_node(self.btree[1], 1, None, None, None)) self.btree.insert(4, 1) # 1 # / # 4 self.assertTrue(test_binary_node(self.btree[1], 1, 4, None, None)) self.assertTrue(test_binary_node(self.btree[4], 4, None, None, 1)) self.btree.insert(3, 1) # 1 # / \ # 4 3 self.assertTrue(test_binary_node(self.btree[1], 1, 4, 3, None)) self.assertTrue(test_binary_node(self.btree[4], 4, None, None, 1)) self.assertTrue(test_binary_node(self.btree[3], 3, None, None, 1)) self.btree.insert(2, 1, 4) # 1 # / \ # 2 3 # / # 4 self.assertTrue(test_binary_node(self.btree[1], 1, 2, 3, None)) self.assertTrue(test_binary_node(self.btree[2], 2, 4, None, 1)) self.assertTrue(test_binary_node(self.btree[3], 3, None, None, 1)) self.assertTrue(test_binary_node(self.btree[4], 4, None, None, 2)) # Binary Search Tree def test_binary_search_node(self): root = binary_search_tree() for i in range(1, 9): self.assertTrue(root.search(i)) self.assertFalse(root.search(0)) self.assertFalse(root.search(9)) def test_binary_search_insert(self): # 5 # / \ # 3 8 # / \ / # 2 4 6 root = BinarySearchNode(5) self.assertTrue(test_binary_node(root, 5, None, None, None)) root.insert(3) self.assertTrue(test_binary_node(root, 5, 3, None, None)) self.assertTrue(test_binary_node(root[3], 3, None, None, 5)) root.insert(8) self.assertTrue(test_binary_node(root, 5, 3, 8, None)) self.assertTrue(test_binary_node(root[3], 3, None, None, 5)) self.assertTrue(test_binary_node(root[8], 8, None, None, 5)) root.insert(4) self.assertTrue(test_binary_node(root, 5, 3, 8, None)) self.assertTrue(test_binary_node(root[3], 3, None, 4, 5)) self.assertTrue(test_binary_node(root[8], 8, None, None, 5)) self.assertTrue(test_binary_node(root[4], 4, None, None, 3)) root.insert(2) self.assertTrue(test_binary_node(root, 5, 3, 8, None)) self.assertTrue(test_binary_node(root[3], 3, 2, 4, 5)) self.assertTrue(test_binary_node(root[8], 8, None, None, 5)) self.assertTrue(test_binary_node(root[2], 2, None, None, 3)) self.assertTrue(test_binary_node(root[4], 4, None, None, 3)) root.insert(6) self.assertTrue(test_binary_node(root, 5, 3, 8, None)) self.assertTrue(test_binary_node(root[3], 3, 2, 4, 5)) self.assertTrue(test_binary_node(root[8], 8, 6, None, 5)) self.assertTrue(test_binary_node(root[2], 2, None, None, 3)) self.assertTrue(test_binary_node(root[4], 4, None, None, 3)) self.assertTrue(test_binary_node(root[6], 6, None, None, 8)) # def test_binary_search_remove_external_node(self): # root = binary_search_tree() # root.remove(1) # self.assertTrue(test_binary_node(root, 5, 3, 8, None)) # self.assertTrue(test_binary_node(root[3], 3, 2, 4, 5)) # self.assertTrue(test_binary_node(root[8], 8, 6, None, 5)) # self.assertTrue(test_binary_node(root[2], 2, None, None, 3)) # self.assertTrue(test_binary_node(root[4], 4, None, None, 3)) # self.assertTrue(test_binary_node(root[6], 6, None, 7, 8)) # self.assertTrue(test_binary_node(root[7], 7, None, None, 6)) # self.assertFalse(1 in root) # self.assertFalse(root.search(1)) def test_binary_search_remove_internal_node_without_children(self): root = binary_search_tree() root.remove(3, root[4]) self.assertTrue(test_binary_node(root, 5, 4, 8, None)) self.assertTrue(test_binary_node(root[4], 4, 2, None, 5)) self.assertTrue(test_binary_node(root[8], 8, 6, None, 5)) self.assertTrue(test_binary_node(root[2], 2, 1, None, 4)) self.assertTrue(test_binary_node(root[6], 6, None, 7, 8)) self.assertTrue(test_binary_node(root[7], 7, None, None, 6)) self.assertFalse(3 in root) def test_binary_search_remove_internal_node_with_children1(self): root = binary_search_tree() root.insert(3.5) root.insert(4.5) root.remove(3, root[4]) self.assertTrue(test_binary_node(root, 5, 4, 8, None)) self.assertTrue(test_binary_node(root[4], 4, 3.5, 4.5, 5)) self.assertTrue(test_binary_node(root[8], 8, 6, None, 5)) self.assertTrue(test_binary_node(root[2], 2, 1, None, 3.5)) self.assertTrue(test_binary_node(root[6], 6, None, 7, 8)) self.assertTrue(test_binary_node(root[1], 1, None, None, 2)) self.assertTrue(test_binary_node(root[3.5], 3.5, 2, None, 4)) self.assertTrue(test_binary_node(root[4.5], 4.5, None, None, 4)) self.assertTrue(test_binary_node(root[7], 7, None, None, 6)) self.assertFalse(3 in root) def test_binary_search_remove_internal_node_with_children2(self): root = binary_search_tree() root.insert(3.5) root.insert(4.5) root.remove(3, root[2]) self.assertTrue(test_binary_node(root, 5, 2, 8, None)) self.assertTrue(test_binary_node(root[2], 2, 1, 4, 5)) self.assertTrue(test_binary_node(root[8], 8, 6, None, 5)) self.assertTrue(test_binary_node(root[6], 6, None, 7, 8)) self.assertTrue(test_binary_node(root[1], 1, None, None, 2)) self.assertTrue(test_binary_node(root[4], 4, 3.5, 4.5, 2)) self.assertTrue(test_binary_node(root[3.5], 3.5, None, None, 4)) self.assertTrue(test_binary_node(root[4.5], 4.5, None, None, 4)) self.assertTrue(test_binary_node(root[7], 7, None, None, 6)) self.assertFalse(3 in root) # HEAP def test_heap_iteration(self): heap = Heap(max_heap()) self.assertEqual([int(str(n)) for n in heap.breadth()], [17, 15, 10, 6, 10, 7]) self.assertEqual([int(str(n)) for n in heap], [17, 15, 10, 6, 10, 7]) self.assertEqual(heap.flatten(), [17, 15, 10, 6, 10, 7]) def test_find_open_with_empty_aunt(self): node17 = HeapNode(17) node15 = HeapNode(15) node11 = HeapNode(11) node6 = HeapNode(6) node10 = HeapNode(10) node17.left = node15 node17.right = node11 node15.left = node6 node15.right = HeapNode(10) heap = Heap(node17) self.assertEqual(heap[-1].value, 10) self.assertEqual(heap.find_open(), (node11, 'left')) def test_find_open_with_single_child_aunt(self): node17 = HeapNode(17) node15 = HeapNode(15) node11 = HeapNode(11) node6 = HeapNode(6) node10 = HeapNode(10) node7 = HeapNode(7) node17.left = node15 node17.right = node11 node15.left = node6 node15.right = HeapNode(10) node11.left = node7 heap = Heap(node17) self.assertEqual(heap[-1].value, 7) self.assertEqual(heap.find_open(), (node11, 'right')) def test_find_open_with_non_empty_aunt(self): node17 = HeapNode(17) node15 = HeapNode(15) node11 = HeapNode(11) node6 = HeapNode(6) node10 = HeapNode(10) node7 = HeapNode(7) node5 = HeapNode(5) node17.left = node15 node17.right = node11 node15.left = node6 node15.right = HeapNode(10) node11.left = node7 node11.right = node5 heap = Heap(node17) self.assertEqual(heap[-1].value, 5) self.assertEqual(heap.find_open(), (node6, 'left')) def test_insert(self): node17 = HeapNode(17) heap = Heap(node17) heap.insert(15) self.assertTrue(test_binary_node(heap.root, 17, 15, None, None)) self.assertTrue(test_binary_node(heap.root.left, 15, None, None, 17)) heap.insert(10) self.assertTrue(test_binary_node(heap.root, 17, 15, 10, None)) self.assertTrue(test_binary_node(heap.root.left, 15, None, None, 17)) self.assertTrue(test_binary_node(heap.root.right, 10, None, None, 17)) heap.insert(6) self.assertTrue(test_binary_node(heap.root, 17, 15, 10, None)) self.assertTrue(test_binary_node(heap.root.left, 15, 6, None, 17)) self.assertTrue(test_binary_node(heap.root.right, 10, None, None, 17)) self.assertTrue(test_binary_node(heap.root.left.left, 6, None, None, 15)) heap.insert(10) self.assertTrue(test_binary_node(heap.root, 17, 15, 10, None)) self.assertTrue(test_binary_node(heap.root.left, 15, 6, 10, 17)) self.assertTrue(test_binary_node(heap.root.right, 10, None, None, 17)) self.assertTrue(test_binary_node(heap.root.left.left, 6, None, None, 15)) self.assertTrue(test_binary_node(heap.root.left.right, 10, None, None, 15)) heap.insert(7) self.assertTrue(test_binary_node(heap.root, 17, 15, 10, None)) self.assertTrue(test_binary_node(heap.root.left, 15, 6, 10, 17)) self.assertTrue(test_binary_node(heap.root.right, 10, 7, None, 17)) self.assertTrue(test_binary_node(heap.root.left.left, 6, None, None, 15)) self.assertTrue(test_binary_node(heap.root.left.right, 10, None, None, 15)) self.assertTrue(test_binary_node(heap.root.right.left, 7, None, None, 10)) def test_bubble(self): # 17 17 100 # / \ / \ / \ # 15 10 --> 15 100 --> 15 17 # / \ / \ / \ / \ / \ / \ # 6 10 7 100 6 10 7 10 6 10 7 10 heap = Heap(max_heap()) parent = heap.root.right last = HeapNode(100) parent.right = last last.parent = parent heap.bubble(last) self.assertTrue(test_binary_node(heap.root, 100, 15, 17, None)) self.assertTrue(test_binary_node(heap.root.right, 17, 7, 10, 100)) self.assertTrue(test_binary_node(heap.root.right.left, 7, None, None, 17)) self.assertTrue(test_binary_node(heap.root.right.right, 10, None, None, 17)) # 100 100 100 # / \ / \ / \ # 15 17 --> 15 17 --> 99 17 # / \ / \ / \ / \ / \ / \ # 6 10 7 10 99 10 7 10 15 10 7 10 # / / / # 99 6 6 parent = heap.root.left.left last = HeapNode(99) last.parent = parent parent.left = last heap.bubble(last) self.assertTrue(test_binary_node(heap.root, 100, 99, 17, None)) self.assertTrue(test_binary_node(heap.root.left, 99, 15, 10, 100)) self.assertTrue(test_binary_node(heap.root.left.left, 15, 6, None, 99)) self.assertTrue(test_binary_node(heap.root.left.right, 10, None, None, 99)) self.assertTrue(test_binary_node(heap.root.left.left.left, 6, None, None, 15)) # 100 100 100 # / \ / \ / \ # 99 17 --> 99 17 --> 100 17 # / \ / \ / \ / \ / \ / \ # 15 10 7 10 15 100 7 10 15 99 7 10 # / \ / / \ / / \ / # 6 12 100 6 12 10 6 12 10 fifteen = heap.root.left.left twelve = HeapNode(12) twelve.parent = fifteen fifteen.right = twelve ten = heap.root.left.right last = HeapNode(100) ten.left = last last.parent = ten heap.bubble(last) self.assertTrue(test_binary_node(heap.root, 100, 100, 17, None)) self.assertTrue(test_binary_node(heap.root.left, 100, 15, 99, 100)) self.assertTrue(test_binary_node(heap.root.left.left, 15, 6, 12, 100)) self.assertTrue(test_binary_node(heap.root.left.right, 99, 10, None, 100)) def test_insertion_with_percolation(self): # 17 # / \ # 15 10 # / \ / # 6 10 7 # 6 six = HeapNode(6) heap = Heap(six) self.assertTrue(test_binary_node(heap.root, 6, None, None, None)) # 6 10 # / --> / # 10 6 heap.insert(10) self.assertTrue(test_binary_node(heap.root, 10, 6, None, None)) self.assertTrue(test_binary_node(heap.root.left, 6, None, None, 10)) # 10 10 # / \ --> / \ # 6 7 6 7 heap.insert(7) self.assertTrue(test_binary_node(heap.root, 10, 6, 7, None)) self.assertTrue(test_binary_node(heap.root.left, 6, None, None, 10)) self.assertTrue(test_binary_node(heap.root.right, 7, None, None, 10)) # 10 15 # / \ --> / \ # 6 7 10 7 # / / # 15 6 heap.insert(15) self.assertTrue(test_binary_node(heap.root, 15, 10, 7, None)) self.assertTrue(test_binary_node(heap.root.left, 10, 6, None, 15)) self.assertTrue(test_binary_node(heap.root.right, 7, None, None, 15)) self.assertTrue(test_binary_node(heap.root.left.left, 6, None, None, 10)) # 15 17 # / \ --> / \ # 10 7 15 7 # / \ / \ # 6 17 6 10 heap.insert(17) self.assertTrue(test_binary_node(heap.root, 17, 15, 7, None)) self.assertTrue(test_binary_node(heap.root.left, 15, 6, 10, 17)) self.assertTrue(test_binary_node(heap.root.right, 7, None, None, 17)) self.assertTrue(test_binary_node(heap.root.left.left, 6, None, None, 15)) self.assertTrue(test_binary_node(heap.root.left.right, 10, None, None, 15)) # 17 17 # / \ --> / \ # 15 7 15 11 # / \ / / \ / # 6 10 11 6 10 7 heap.insert(11) self.assertTrue(test_binary_node(heap.root, 17, 15, 11, None)) self.assertTrue(test_binary_node(heap.root.left, 15, 6, 10, 17)) self.assertTrue(test_binary_node(heap.root.right, 11, 7, None, 17)) self.assertTrue(test_binary_node(heap.root.left.left, 6, None, None, 15)) self.assertTrue(test_binary_node(heap.root.left.right, 10, None, None, 15)) self.assertTrue(test_binary_node(heap.root.right.left, 7, None, None, 11)) def test_build_from_array(self): heap1 = [17, 15, 11, 6, 10, 7] heap2 = [6, 10, 7, 15, 17, 11] heap = Heap(array=heap1) self.assertTrue(test_binary_node(heap.root, 17, 15, 11, None)) self.assertTrue(test_binary_node(heap.root.left, 15, 6, 10, 17)) self.assertTrue(test_binary_node(heap.root.right, 11, 7, None, 17)) self.assertTrue(test_binary_node(heap.root.left.left, 6, None, None, 15)) self.assertTrue(test_binary_node(heap.root.left.right, 10, None, None, 15)) self.assertTrue(test_binary_node(heap.root.right.left, 7, None, None, 11)) heap = Heap(array=heap2) self.assertTrue(test_binary_node(heap.root, 17, 15, 11, None)) self.assertTrue(test_binary_node(heap.root.left, 15, 6, 10, 17)) self.assertTrue(test_binary_node(heap.root.right, 11, 7, None, 17)) self.assertTrue(test_binary_node(heap.root.left.left, 6, None, None, 15)) self.assertTrue(test_binary_node(heap.root.left.right, 10, None, None, 15)) self.assertTrue(test_binary_node(heap.root.right.left, 7, None, None, 11)) def test_delete(self): # 17 7 15 15 # / \ / \ / \ / \ # 15 11 --> 15 11 --> 7 11 --> 10 11 # / \ / / \ / \ / \ # 6 10 7 6 10 6 10 6 7 heap = Heap(max_heap()) heap[2].value = 11 heap.delete_root() self.assertTrue(test_binary_node(heap.root, 15, 10, 11, None)) self.assertTrue(test_binary_node(heap.root.left, 10, 6, 7, 15)) self.assertTrue(test_binary_node(heap.root.right, 11, None, None, 15)) self.assertTrue(test_binary_node(heap.root.left.left, 6, None, None, 10)) self.assertTrue(test_binary_node(heap.root.left.right, 7, None, None, 10)) # 15 7 11 # / \ / \ / \ # 10 11 --> 10 11 --> 10 7 # / \ / / # 6 7 6 6 heap.delete_root() self.assertTrue(test_binary_node(heap.root, 11, 10, 7, None)) self.assertTrue(test_binary_node(heap.root.left, 10, 6, None, 11)) self.assertTrue(test_binary_node(heap.root.right, 7, None, None, 11)) self.assertTrue(test_binary_node(heap.root.left.left, 6, None, None, 10)) # 11 6 10 # / \ / \ / \ # 10 7 --> 10 7 --> 6 7 # / # 6 heap.delete_root() self.assertTrue(test_binary_node(heap.root, 10, 6, 7, None)) self.assertTrue(test_binary_node(heap.root.left, 6, None, None, 10)) self.assertTrue(test_binary_node(heap.root.right, 7, None, None, 10)) # 10 7 # / \ --> / # 6 7 6 heap.delete_root() self.assertTrue(test_binary_node(heap.root, 7, 6, None, None)) self.assertTrue(test_binary_node(heap.root.left, 6, None, None, 7)) # 7 # / --> 6 # 6 heap.delete_root() self.assertTrue(test_binary_node(heap.root, 6, None, None, None)) # 6 --> None heap.delete_root() self.assertFalse(heap.root)
def hot_potato(names_lst, num): """ queue data structure implementation returns the last person surviving in the queue """ names = Queue() for i in names_lst: # add the names to the queue names.add(i) turns = 1 while names.get_length() > 1: if turns < num: # get and return items to queue potato = names.get() names.add(potato) turns += 1 else: # when turns == num remove item names.get() turns = 1 # get the survivor off of the queue return names.get()
def test_dequeue(self): q = Queue(1) q.enqueue(2) q.enqueue(3) q.enqueue(4) self.assertEqual(len(q), 4) self.assertEqual(q.peek(), 1) q.dequeue() self.assertEqual(len(q), 3) self.assertEqual(q.peek(), 2) while q.storage: q.dequeue() self.assertRaises(IndexError, q.dequeue)
def single_dfs(big_list, current): stack = Stack() explored = Queue() pointer = current stack.push(pointer) explored.enqueue(pointer) tra_model = Tra_model() while True: if stack.isEmpty(): break else: pointer = stack.peek() x = pointer[0] y = pointer[1] if big_list[x][y + 1] == " ": #if right is empty pointer = tra_model.move_right(pointer) stack.push(pointer) explored.enqueue(pointer) x = pointer[0] y = pointer[1] big_list[x][y] = "#" continue #return to the top of the loop if big_list[x][y + 1] == ".": pointer = tra_model.move_right(pointer) stack.push(pointer) explored.enqueue(pointer) break if big_list[x][y + 1] == "%" or "#": #if right is wall if big_list[x + 1][y] == " ": #if down is empty pointer = tra_model.move_down(pointer) stack.push(pointer) explored.enqueue(pointer) x = pointer[0] y = pointer[1] big_list[x][y] = "#" continue if big_list[x + 1][y] == ".": pointer = tra_model.move_down(pointer) stack.push(pointer) explored.enqueue(pointer) break if big_list[x + 1][y] == "%" or "#": # if down is wall if big_list[x - 1][y] == " ": # if up is empty pointer = tra_model.move_up(pointer) stack.push(pointer) explored.enqueue(pointer) x = pointer[0] y = pointer[1] big_list[x][y] = "#" continue # return to the top of the loop if big_list[x - 1][y] == ".": pointer = tra_model.move_up(pointer) stack.push(pointer) explored.enqueue(pointer) break if big_list[x - 1][y] == "%" or "#": #if up is wall if big_list[x][y - 1] == " ": #if left is empty pointer = tra_model.move_left(pointer) stack.push(pointer) explored.enqueue(pointer) x = pointer[0] y = pointer[1] big_list[x][y] = "#" continue #return to the top if big_list[x - 1][y] == ".": pointer = tra_model.move_left(pointer) stack.push(pointer) explored.enqueue(pointer) break if big_list[x][y - 1] == "%" or "#": big_list[x][y] = "*" stack.pop() expanded = 0 for i in explored.items: expanded += 1 x = i[0] y = i[1] if big_list[x][y] == "*": big_list[x][y] = " " steps = 0 for i in explored.items: x = i[0] y = i[1] if big_list[x][y] == "#": steps += 1 return explored, big_list, steps, expanded