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.