0

Hello i'm fighting with one kata on Codewars. My idea is to create list of coordinates based on list of directions(NORTH,SOUTH...) but here a have a problem. I'm going to use loop while, to check every direction and simultaneously appending created coordinates to list t. But I'm surprised because my loop doesn't append coordinates to t just after creating, but add final result of all iterations of the loop. Could you explain me how it works and how can I add result of particular iterartion of loop to list t?

def dirReduc(arr):
    t=[[0,0]]

    a=[0,0]
    b=0
    while b<len(arr):

         if arr[b]=='NORTH':
            print(b)

            a[0]=a[0]+1
            print(a)
            t.append(a)

        b=b+1
    print(t)



print(dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]))
# I wish to modify this to [[0,0],[1,0],[0,0],[-1,0],[-1,1],[-1,0],[0,0],[0,-1]]
# if 'North' [a+1,b], if 'South' [a-1,b],if 'West' [a,b-1],if 'East' [a,b+1]
Run:
0
[1, 0]
5
[2, 0]
[[0, 0], [2, 0], [2, 0]] #here i suspect:[[0, 0], [1, 0], [2, 0]]
None
aneroid
  • 12,983
  • 3
  • 36
  • 66
user13137381
  • 115
  • 3
  • 12

1 Answers1

0

When you are appending a to the list, you are appending the reference (see here), not the "snapshot in time" that you think you are getting. You need to instead use copy():

t.append(a.copy())
cwalvoort
  • 1,851
  • 1
  • 18
  • 19