So this problem was given as an end of the class quiz. I didnt have time to test it out so I tried re-writting it and testing. However for some reason its not working the way I want it and I dont know why.
So you've got a nxn grid given as lists. A robot with an initial starting point given as a tuple. And the path the robot will travel given as "N", "S", "E", "W" within a list.
so input would be something like
make_grid(starting_point, path, size_of_grid)
make_grid((0,0),["S","E","S","S"],4)
with an output like
[".","_","_","_"],[".",".","_","_"],["_",".""_""_"],["_",".""_""_"]
where the "." is the robots starting point and traveled path. The "_" are the untraveled areas of the grid. And if it hits the border it stays in the exact same spot. My problem is the result is that it marks the whole column as a traveled path with a period
def check(coord ,size):
if ((coord<0) or (coord>size)):
return True
else:
return False
def make_grid(start, path, size):
n = 0
row = []
while (n < size):
row.append("_")
n += 1
n = 1
grid = [row]
while ( n < size):
grid.append(row)
n +=1
x = start[0]
y = start[1]
grid[x][y] = "."
n = 0
while (n < len(path)):
if (path[n] == "N"):
x -= 1
if (check(x,size)):
x += 1
elif (path[n] == "E"):
y += 1
if (check(y,size)):
y -= 1
elif (path[n] == "S"):
x +=1
if (check(x,size)):
x -=1
elif (path[n] == "W"):
y -=1
if (check(y,size)):
y += 1
grid[x][y] = "."
n += 1
n = 0
while (n < size):
print grid[n]
n += 1