Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with Python please! Thank you :) def stack_maze(maze): ------------------------------------------------------- Solves a maze using Stack. Use: path = stack_maze(maze) ------------------------------------------------------- Parameters: maze

I need help with Python please! Thank you :)

def stack_maze(maze):

"""

-------------------------------------------------------

Solves a maze using Stack.

Use: path = stack_maze(maze)

-------------------------------------------------------

Parameters:

maze - dictionary of points in a maze, where each point

represents a corridor end or a branch. Dictionary

keys are the name of the point followed by a list of

branches, if any. First point is named 'Start', exit

is named 'X' (dict)

Returns:

points - list of points visited before the exit is reached,

None if there is no exit (list of str)

-------------------------------------------------------

"""

Add the function to a PyDev module named functions.py. Test it from t06.py.

This function uses a stack, meaning you may manipulate the stack using only the stack interface methods such as push, pop, isEmpty, and peek. You may not use or refer to the internal Stack properties such as _values.

The algorithm for solving a maze is discussed in Lesson 4: Stacks - Solving a Maze

image text in transcribed

Figure 2. A Sample Maze. Created by David Brown. Used with permission.

The Sample Maze from the notes is unnecessarily complex. We don't really need to know turns and twists of the maze, just where the decision nodes and end nodes are in relation to each other. We can replace the maze shown in Figure 2, with a simplified version as shown in the following figure:

image text in transcribed

Figure 3. A Simpler Maze. Created by David Brown. Used with permission.

The arrows show how decision points connect forward to other decision points and dead ends. 'A' connects only to 'B' and 'C', and 'C' only to 'D' and 'E', and so on. (Two way connections are possible, but don't help find a solution.)

This maze can be represented by a Python dictionary:

maze = {'Start': ['A'], 'A':['B', 'C'], 'B':[], 'C':['D', 'E'],

'D':[], 'E':['F', 'X'], 'F':['G', 'H'], 'G':[], 'H':[]}

Each entry in this Python dictionary consists of two parts that are separated by a colon, key: value. The key represents an entry point and the value shows all the points connecting to the entry. Therefore, for the sample maze, shown in Figure 2, it's 'Start' entry has key: 'Start' and value: ['A'].

Thus so long as you have a key you can extract its value. In a valid maze dictionary, each entry has a string key (e.g., 'Start', 'A', 'B', etc.) and a list value (e.g., ['A'], ['B', 'C'], [], etc.). Dead ends have empty lists. There is no entry for the special value 'X' as that represents the exit from the maze.

Since dictionaries use a key rather than an index, to extract a value from a dictionary use the syntax:

value = maze[key]

Thus, to extract the list of locations connected to the 'Start' entry of the original maze, write:

key = 'Start'

value = maze[key

and value should contain ['A']. You could then extract the list of entries connected to 'A':

key = 'Start'

value = maze[key]

key = value[0]

value = maze[key]

and value should contain ['B', 'C'], and so on.

The stack_maze function then takes a maze dictionary as a parameter, and returns a list of letters that are visited before we reach the exit of the maze. For the example above, we know from the course notes that the resulting points should be ['A', 'C', 'E', 'X'].

Note: The algorithm discussed in lesson 4 does not return the path from start to exit. Here also you do not need to return the path. You just return the list of points visited. In this example, ['A', 'C', 'E', 'X'] happens to be the path. As mentioned in the lesson, if you push the points into Stack in reverse alphabetical order, you get the list ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'X'] which is not the path from start to exit.

Other sample mazes to test:

A trivial maze:

image text in transcribed

Figure 4. A Trivial Maze. Created by David Brown. Used with permission.

is represented by:

maze = {'Start': ['X']}

A maze with a circular path:

image text in transcribed

Figure 5. A Circular Maze. Created by David Brown. Used with permission.

is represented by:

maze = {'Start': ['A'], 'A':['B', 'C'], 'B':[], 'C':['D', 'E'],

'D':[], 'E': ['X', 'F'], 'F':['G'], 'G':['C']}

and a maze with no exit:

maze = {'Start': ['A'], 'A':[]}

I know this is quite lengthy. Thank you so much.

Start I F B D EX Start HE G A06X 00+ D Start-X Start G6 AC B D Start I F B D EX Start HE G A06X 00+ D Start-X Start G6 AC B D

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database Design A Practical Approach

Authors: Marilyn Campbell

1st Edition

1587193175, 978-1587193170

More Books

Students also viewed these Databases questions