Output a table of size n×n, filled with integers from 1 to n2 (square) spiral coming from the upper left corner and twisted in a clockwise direction, as shown in the example (here n=5):

1 2 3 4 5

16 17 18 19 6

15 24 25 20 7

14 23 22 21 8

13 12 11 10 9

On the second day gave up and found a solution online but did not understand, here's the code:

Please explain the "essence" of matrices in Python

1 2 3 4 5

16 17 18 19 6

15 24 25 20 7

14 23 22 21 8

13 12 11 10 9

On the second day gave up and found a solution online but did not understand, here's the code:

```
n = int(input())
i, j = 0, -1
max_j, max_i = n - 1, n - 1
min_j, min_i = 0, 1
count = 1
mtrx = [[0 for j in range(n)] for i in range(n)]
while True:
while j < max_j:
j += 1
mtrx[i][j] = count
count += 1
max_j -= 1
while i < max_i:
i += 1
mtrx[i][j] = count
count += 1
max_i -= 1
while j > min_j:
j -= 1
mtrx[i][j] = count
count += 1
min_j += 1
while i > min_i:
i -= 1
mtrx[i][j] = count
count += 1
min_i += 1
if j == (n - 1) // 2 and i == n // 2:
break
print()
print()
for i in range(n):
for j in range(n):
print(mtrx[i][j], end = ' ')
print()
```

Please explain the "essence" of matrices in Python

asked June 3rd 19 at 20:20

1 answer

answered on June 3rd 19 at 20:22

Just like everywhere else

The matrix is a multidimensional array

Find more questions by tags PythonMathematical matrix