While trying to create a N by N spiral matrix for number 1 to square(N) , using the usual algorithm for spiral matrix , there is an unexpected output in one of the rows which cannot be found even on rechecking.
def getSpiralOrder(N):
matrix = [ [ 0 for i in range(N) ] for j in range(N) ]
c = 1
rS = 0
rE = len(matrix)
cS = 0
cE = len(matrix[0])
while(rS < rE and cS < cE):
for i in range(cS , cE ):
matrix[rS][i]=c
c = c + 1
rS += 1
for i in range(rS , rE):
matrix[i][cE - 1]=c
c = c + 1
cE -= 1
for i in range(cS , cE):
matrix[rE - 1][cE - i - 1]=c
c =c + 1
rE -= 1
for i in range(rS,rE):
matrix[rE - i ][cS]=c
c = c + 1
cS += 1
return(matrix)
n = int(input())
print(getSpiralOrder(n))
Output should be: [[1, 2, 3, 4], [12, 13, 14, 5], [11, 16 , 15, 6], [10, 9, 8, 7]]
But output coming is: [[1, 2, 3, 4], [12, 13, 14, 5], [16, 0, 15, 6], [10, 9, 8, 7]]
Turns out all rows are correct except the third one.