0

The function called findStart() is suppose to search through a 2-dimensional list recursively which represents a maze. It's suppose to search through this list and find the x & y (1st and 2nd indices) of a specific element in the list being "S". However it's suppose to use recursion and the findStart() method is to not take any arguments. I've found out how to do it with iteration but how can I make this iteration into recursion?

def findStart():
    for a in mazeList:
        for x in a:
            if x == "S":
                xcoord = a.index("S")
                ycoord = mazeList.index(a)
                return (ycoord),(xcoord)
    return -1,-1

The maze:

####################################
#S#  ##  ######## # #      #     # #
# #   #             # #        #   #
#   # ##### ## ###### # #######  # #
### # ##    ##      # # #     #### #
#   #    #  #######   #   ###    #E#
####################################
user3373360
  • 93
  • 1
  • 11
  • 1
    These are some really weird requirements. If a function has no parameters, how do you determine the terminating condition of the recursion? You'd have to use global variables. Seems like way more trouble than just finding the coordinates iteratively. – Kevin Mar 28 '14 at 17:25
  • You could simply create a second (even inner) function that does the recursion. – Ferdinand Beyer Mar 28 '14 at 17:27
  • I'm not really familiar with Python, but recursion really requires some sort of parameter to be passed in (unless you are using global variables, but that's kind of a bad habit IMO). The parameter is then used to determine if the function needs to be recursively called again or not. The only way I can think to do it is if you create a second method that `findStart()` calls and it's the second method that is recursive (this second method of course having parameters, probably the next coordinates). – Josh Braun Mar 28 '14 at 17:33
  • 1
    Someone posted a very similar question yesterday (same maze, it was python), it might be helpful: http://stackoverflow.com/q/22700130/2282538 – Tyler Mar 28 '14 at 17:34

1 Answers1

0

Now, to preface this I agree with the comment discussion above that doing recursive searching in python on a function without arguments is not recommended because you could run into problems depending on how your search works and how it accesses your global varaibles, and you would essentially be doing an iterative search that is "governed" by a function, i.e. it just decides what to increment and when.

To properly do an iterative search in the fashion you describe you could turn findStart into a wrapper function:

either (reccomended):

def findStart(mazeList):
    return findStartRec(mazeList,0,0)

or: mazeList = ... # define mazeList def findStart(mazeList): return findStartRec(mazeList,0,0)

and then solving:

def findStartRec(mazeList,x,y):
    if y >= len(mazeList):
        return -1,-1                         # We have reached the end of the maze and no result
    if x >= len(mazeList[y]):
        return findStartRec(mazeList,0,y+1)  # We have reached the end of a row, continue to the next row
    if mazeLise[y][x] == "S":
        return x,y                           # We have found the start
    else:
        return findStartRec(mazeList,x+1,y)  # Search the next spot on this row

Works for me:

>>> findStartRec(mazeList) 
(1, 1)

And then with the definition first, no argument function:

maze = """####################################
#S#  ##  ######## # #      #     # #
# #   #             # #        #   #
#   # ##### ## ###### # #######  # #
### # ##    ##      # # #     #### #
#   #    #  #######   #   ###    #E#
####################################"""
mazeList = [[x for x in row] for row in maze.split('\n')]
def findStart():
    return findStartRecu(mazeList,0,0)

and then calling:

>>> findStart()
(1,1)  

As a final note, this is not really the best application for recursive searching as this search exists in very definitive and known bounds, i.e. it is rectangular in shape. Recursive searching is better suited for things like trees, linked lists, etc. data structures of a non-linear shape because it is not possible to really search them using a finite mapping like a for loop.

Farmer Joe
  • 6,020
  • 1
  • 30
  • 40