I was solving a question of matrix rotation by 90 degrees. In the problem, I am taking up a list k
filled with 0s of the exact degree as passed by the user.
I have tried this following code:
def rotate(m):
k=[]
f=[]
print(m)
for i in range(0,len(m)):
f.append(0)
for i in range(0,len(m)):
k.append(f)
print(k)
for i in range(0,len(m)):
for j in range(0,len(m)):
print("REPLACING POSITION:",i,j )
t=m[i][j]
k[j][len(m)-i-1]=t
return (k)
print(rotate([[1,2],[3,4]]))
I expect the output:
[[1, 2], [3, 4]]
[[0, 0], [0, 0]]
REPLACING POSITION: 0 0
REPLACING POSITION: 0 1
REPLACING POSITION: 1 0
REPLACING POSITION: 1 1
[[3, 1], [4, 2]]
I am getting the output:
[[1, 2], [3, 4]]
[[0, 0], [0, 0]]
REPLACING POSITION: 0 0
REPLACING POSITION: 0 1
REPLACING POSITION: 1 0
REPLACING POSITION: 1 1
[[4, 2], [4, 2]]
Why does the last row keep on repeating? Please help.