I have this two versions of a program that generates a magic square of odd order,
1. using numpy arrays :
import numpy as np
N = 5
magic_square = np.zeros((N, N), dtype=int)
n = 1
i, j = 0, N // 2
while n <= N ** 2:
magic_square[i, j] = n
n += 1
newi, newj = (i - 1) % N, (j + 1) % N
if magic_square[newi, newj]:
i += 1
else:
i, j = newi, newj
print(magic_square)
2. using lists
N = 5
magic_square = [[0] * N] * N
n = 1
i, j = 0, N // 2
while n <= N ** 2:
magic_square[i][j] = n
n += 1
newi, newj = (i - 1) % N, (j + 1) % N
if magic_square[newi][newj]:
i += 1
else:
i, j = newi, newj
print(magic_square)
But the one which uses lists gives an IndexError: list index out of range for line , magic_square[i][j] = n
Is there any difference in indexing for np arrays and lists ?
If yes, then how do I correct the code containing lists ?