i'm solving a leetcode question using 2 d array in python. When i try to put the element in a specific location in 2d array , it is inserting in all the corresponding column.
For example if i try to insert an element in 1st row 1st column, it is inserting the element in rows in first column.
In the code line : a[i][j] = s[si]
Can anyone help me how to get rid off and help me tell why this is happening ?
This is the code:
class Solution:
def convert(self, s: str, numRows: int) -> str:
n = len(s)
m = math.ceil(n/(2*numRows-1))
m = int(m)
col = m*n
a = [[0]*col]*numRows
i = 0
j = 0
si = 0
up = False
down = True
while(si<n):
for _ in range(numRows):
if(down and si<n):
a[i][j] = s[si]
#print(a[i][j])
print("down si",s[si],i,j)
print(a)
i = i+1
si = si+1
if((i) == numRows ):
#print("array before up",a)
i = numRows-2
j = j+1
up = True
down = False
for _ in range(numRows):
if(up):
if(i==0):
i = 0
up = False
down = True
else:
a[i][j] = s[si]
#print("up si",s[si],i,j)
#print(a)
si = si+1
i = i-1
j = j+1
print(a)