-1

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
aslansutu
  • 11
  • 4
  • 1
    Welcome to Stack Overflow. We do expect some effort here. The least you could do is explain exactly what is wrong and what you've tried to find out the cause. – Thijs van Dien Dec 21 '17 at 01:04
  • ah sorry. Ive tried to make the grid smaller, tried to have it print out the grid at certain times. The problem is the result. While i am hoping for a result like it is above i get a result like this ['.', '.', '_', '_'] ['.', '.', '_', '_'] ['.', '.', '_', '_'] ['.', '.', '_', '_'] where the whole column is marked as path taken – aslansutu Dec 21 '17 at 01:07
  • You said `the "." is the robots starting point` but there are multiple periods in your sample output. Do you mean the period marks a spot where the robot has visited? – Rory Daulton Dec 21 '17 at 01:07
  • @RoryDaulton yup. the period is where the robot has traveled. Ive also marked the initial starting point with a period – aslansutu Dec 21 '17 at 01:11
  • @Wombatz you are correct. I just didnt know that was my problem. – aslansutu Dec 21 '17 at 14:17

1 Answers1

0

Your initialization of each row in grid is setting each row to the same list. So any change to an element in grid will change every row (since they are all the same list). You need to set each element of grid to a separate object. Something like:

grid = []
    for i in range(size):
        grid.append([])
        for j in range(size):
            grid[i].append('_')

I'm sure a more experienced Python programmer can come up with a more compact initialization.

John Anderson
  • 35,991
  • 4
  • 13
  • 36
  • 1
    If that's the problem, it suffices to use `grid.append(row[:])` (i.e. a copy of the row) instead of `grid.append(row)` (i.e. another alias). – Thijs van Dien Dec 21 '17 at 02:28
  • Thanks mate this made it work out the way I wanted it to. Didnt realize python was a call by reference – aslansutu Dec 21 '17 at 14:18