PYTHON EXPERT ONLY

profileJim_Ak
maze_solver_-_rubric.zip

Maze Solver - Rubric.xlsx

Sheet1

Maze Solver - Rubric
Student Name:
For each evaluation criteria, fill out the deserved number of marks (out of the total shown) :
Description Self assessment marks out of Teacher evaluation
load_maze function
the function returns two-dimentional list 1
the returned list is exact representation of the input file - each character is an element 1
the 'new line' characters are not included in the returned list 1
the function does not alter the input file 1
pick_random_location function
the function returns a tuple of two integer numbers - column and row 1
the chosen location falls inside the maze 1
the chosen location is an empty alley spot 1
print_maze function
the function prints arbitrary 2D array of single characters 1
the elements from each nested list are agregated into a string 1
strings are printed on separate lines, one after another, no spacing between them 1
find_path RECURSIVE function
the base cases are propely defined 3
the function returns result when a base cases is encountered 2
the function marks current location with a '+' sign, as part of the path 1
the function calls itself recursively for all surrounding cells 4
the function unmarks current location as part of the path if there is no path via any of the surrounding cells 1
Application of concepts
the program visualizes the process of solving the maze by drawing it at each step 3
the program uses properly two-dimensional list 2
the program converts efficiently string to list and vice versa 2
Overall
functionality 4
program appearance (header/comments/docstrings/names/spacing) 4
TOTAL 0 36 0
Comments:

Sheet2

Sheet3

maze_generator (1).py

######################################### # Programmer: Nathan Moore # Adaptation: Mr.G # File Name: maze_generator.py # Description: This program generates a maze of arbitrary size and saves it in a file. # Source: # http://natewm.com/blog/2012/01/python-recursive-maze-example/ ######################################### import random def makeMaze(width, height): # maze dimensions are doubled, to include the walls maze = [[0 for j in xrange(width*2)] for i in xrange(height*2)] recurseMaze(maze, (width / 2) * 2, (height / 2) * 2, 0, 0) return maze # begin recursion starting in the center def recurseMaze(maze, x, y, dirx, diry): if not 0 <= y < len(maze) or not 0 <= x < len(maze[0]) or maze[y][x] != 0: return # base case: returns if reaches the borders # or if current location is not a wall maze[y-diry][x-dirx] = 1 # maze[y][x] = 1 # mark current location and the previous one as alley directions = [(1,0), (-1,0), (0,1), (0,-1)] random.shuffle(directions) for dx, dy in directions: # recurse in the four directions recurseMaze(maze, x + dx * 2, y + dy * 2, dx, dy) def mazeString(maze, chars): # converts zeroes to walls and ones to alleys s = chars[0] * (len(maze[0]) + 1) + "\n" # adds border at the top for row in maze: # adds border to the left s += chars[0] for cell in row: s += chars[cell] s += "\n" return s #---------------------------------------# # main program # #---------------------------------------# width = int(raw_input("Enter width (number of alleys): ")) height = int(raw_input("Enter height (number of alleys): ")) maze = makeMaze(width,height) print '\nHere is the maze as a list of zeroes and ones:' print maze maze_string = mazeString(maze, ("#", " ")) print '\nHere is the maze as a string of hashtags and spaces:' print maze_string fname = raw_input("\nEnter filename: ") file_out = open(fname,'w') file_out.write(maze_string) file_out.close()

maze_solver_demo.zip

solution and iterations/maze.pyc

solution and iterations/maze1.txt

############################### # # # # # # # # # ##### # ##### # ### ### # # # # # # # # # # # ### ######### # # ####### # # # # # # # # # # ### # # # ######### ### # ### # # # # # # # # # # # ### ### # # ### ### ##### # # # # # # # # # # # # # # ### # # # ### # ### ### # ### # # # # # # # # # # # # ### ### # ######### # # # ### # # # # # # # # # # # # ### ### # ### # # ### # # # # # # # # # # ###############################

solution and iterations/maze2.txt

##################### # # # # ### # ##### ### # # # # # # # # # # ##### ##### ##### # # # # # # # # # # # # # # ##### # # # # # # # # # # # ##### # # # # # # # # # # # # # # # # # # # ### ##### # ### # # # # # # # # # ### # ######### # # # # # # # # # # ##### # ####### # # # # # # # ### ##### # # # ### # # # # # # # # ##### ########### # # # #####################

solution and iterations/maze_solver_main.py

######################################### # Programmer: Mr. G # Date: 28.02.2013 # File Name: maze_solver-main.py # Description: This program solves a maze of arbitrary size. # The program follows the algorithm described on the following website: # http://www.cs.bu.edu/teaching/alg/maze/ # Input file must comply with the following guidelines: # - walls are one character thick and represented with "#" # - alleys are one character wide and represented with spaces # - each line, including the last, ends with 'new line' character # Module maze contains the following functions: # - load_maze(fname) # - pick_random_location(maze) # - print_maze(maze) # - find_path(maze, x, y) # After importing the module, use help(function name), to understand how they work. # These functions exercise the following: # - reading from a file: # - nested lists (2D lists) # - string.join method: # L = ['i', 't', 'e', 'r', 'a', 'b', 'l', 'e'] # print ''.join(L) # - list comprehension # - recursion ######################################### import random from maze import * #---------------------------------------# # main program # #---------------------------------------# fname = raw_input("Enter filename: ") maze = load_maze(fname) # generate random start and goal locations Sx,Sy = pick_random_location(maze) maze[Sy][Sx] = 'S' Gx,Gy = pick_random_location(maze) maze[Gy][Gx] = 'G' print '\nHere is the maze with start and goal locations:' print_maze(maze) # now, find the path from S to G find_path(maze, Sx, Sy) print '\nHere is the maze with the path from start to goal:' print_maze(maze)

solution only/maze.pyc

solution only/maze1.txt

############################### # # # # # # # # # ##### # ##### # ### ### # # # # # # # # # # # ### ######### # # ####### # # # # # # # # # # ### # # # ######### ### # ### # # # # # # # # # # # ### ### # # ### ### ##### # # # # # # # # # # # # # # ### # # # ### # ### ### # ### # # # # # # # # # # # # ### ### # ######### # # # ### # # # # # # # # # # # # ### ### # ### # # ### # # # # # # # # # # ###############################

solution only/maze2.txt

##################### # # # # ### # ##### ### # # # # # # # # # # ##### ##### ##### # # # # # # # # # # # # # # ##### # # # # # # # # # # # ##### # # # # # # # # # # # # # # # # # # # ### ##### # ### # # # # # # # # # ### # ######### # # # # # # # # # # ##### # ####### # # # # # # # ### ##### # # # ### # # # # # # # # ##### ########### # # # #####################

solution only/maze_solver_main.py

######################################### # Programmer: Mr. G # Date: 28.02.2013 # File Name: maze_solver-main.py # Description: This program solves a maze of arbitrary size. # The program follows the algorithm described on the following website: # http://www.cs.bu.edu/teaching/alg/maze/ # Input file must comply with the following guidelines: # - walls are one character thick and represented with "#" # - alleys are one character wide and represented with spaces # - each line, including the last, ends with 'new line' character # Module maze contains the following functions: # - load_maze(fname) # - pick_random_location(maze) # - print_maze(maze) # - find_path(maze, x, y) # After importing the module, use help(function name), to understand how they work. # These functions exercise the following: # - reading from a file: # - nested lists (2D lists) # - string.join method: # L = ['i', 't', 'e', 'r', 'a', 'b', 'l', 'e'] # print ''.join(L) # - list comprehension # - recursion ######################################### import random from maze import * #---------------------------------------# # main program # #---------------------------------------# fname = raw_input("Enter filename: ") maze = load_maze(fname) # generate random start and goal locations Sx,Sy = pick_random_location(maze) maze[Sy][Sx] = 'S' Gx,Gy = pick_random_location(maze) maze[Gy][Gx] = 'G' print '\nHere is the maze with start and goal locations:' print_maze(maze) # now, find the path from S to G find_path(maze, Sx, Sy) print '\nHere is the maze with the path from start to goal:' print_maze(maze)

Reading from TXT file.zip

input.txt

This is a test file. Here is some data: 1 2 3 4 5 And here is some more data: a b c d e

read_file.py

############################### # Programmer: Mr. G # Date: 03-Mar-2012 # File Name: readfile.py # Description: This program prints a file to the screen using 4 different approaches. ############################### fname = raw_input("Enter filename: ") #-----------------------------# file_in = open(fname,'r') # read() data = file_in.read() # Returns one string containing the entire file. print data # file_in.close() # #-----------------------------# file_in = open(fname,'r') # readln() line = " " # Returns a string containing the next line from the file. while (line != ""): # When the function returns an empty string (""), line = file_in.readline() # the end of the file has been reached. line = line[0:-1] # Newline characters are removed before printing print line # the line. file_in.close() # #-----------------------------# file_in = open(fname,'r') # readlines() lines = file_in.readlines() # Returns a list with each line as an element. for i in range(len(lines)): # There will be newline characters (\n) at the end print lines[i] # of each of the strings. file_in.close() # #-----------------------------# file_in = open(fname,'r') # readlines() lines = file_in.readlines() # for i in range(len(lines)): # lines[i]=lines[i][0:-1] # Newline characters are removed before printing print lines[i] # the list elements. file_in.close() # #-----------------------------#

Unconfirmed 540125.crdownload

######################################### # Programmer: Nathan Moore # Adaptation: Mr.G # File Name: maze_generator.py # Description: This program generates a maze of arbitrary size and saves it in a file. # Source: # http://natewm.com/blog/2012/01/python-recursive-maze-example/ ######################################### import random def makeMaze(width, height): # maze dimensions are doubled, to include the walls maze = [[0 for j in xrange(width*2)] for i in xrange(height*2)] recurseMaze(maze, (width / 2) * 2, (height / 2) * 2, 0, 0) return maze # begin recursion starting in the center def recurseMaze(maze, x, y, dirx, diry): if not 0 <= y < len(maze) or not 0 <= x < len(maze[0]) or maze[y][x] != 0: return # base case: returns if reaches the borders # or if current location is not a wall maze[y-diry][x-dirx] = 1 # maze[y][x] = 1 # mark current location and the previous one as alley directions = [(1,0), (-1,0), (0,1), (0,-1)] random.shuffle(directions) for dx, dy in directions: # recurse in the four directions recurseMaze(maze, x + dx * 2, y + dy * 2, dx, dy) def mazeString(maze, chars): # converts zeroes to walls and ones to alleys s = chars[0] * (len(maze[0]) + 1) + "\n" # adds border at the top for row in maze: # adds border to the left s += chars[0] for cell in row: s += chars[cell] s += "\n" return s #---------------------------------------# # main program # #---------------------------------------# width = int(raw_input("Enter width (number of alleys): ")) height = int(raw_input("Enter height (number of alleys): ")) maze = makeMaze(width,height) print '\nHere is the maze as a list of zeroes and ones:' print maze maze_string = mazeString(maze, ("#", " ")) print '\nHere is the maze as a string of hashtags and spaces:' print maze_string fname = raw_input("\nEnter filename: ") file_out = open(fname,'w') file_out.write(maze_string) file_out.close()