How to use python to output the following print matrices?

enter n (0 < n < = 20) and output the following square matrix (for example, in the case of nasty 4):

13 4 10
25 9 11
6 8 12 15
7 13 14 16

Mar.05,2021

import numpy as np


def num(i=[0]):
    i[0] += 1
    return i[0]


shu_ru = int(input("shu ru "))
a = np.zeros((shu_ru, shu_ru))
k = 1
all = []

while k < shu_ru * 2:
    lst = []
    for i in range(k):
        for j in range(k):
            if i + j == k - 1 and i < shu_ru and j < shu_ru:
                lst.append((i, j))
    if k % 2 == 0:
        lst.reverse()
    k += 1
    all.append(lst_p)

for i in all:
    for j in i:
        a[j] = num()

when a = 3, output

Out[1]: 
array([[1., 3., 4.],
       [2., 5., 8.],
       [6., 7., 9.]])

when a = 4, output

Out[2]: 
array([[ 1.,  3.,  4., 10.],
       [ 2.,  5.,  9., 11.],
       [ 6.,  8., 12., 15.],
       [ 7., 13., 14., 16.]])

when a = 1, output

Out[2]: array([[1.]])
The

code is relatively straightforward and not optimized, probably starting with the relationship between the subscript and your input value.


n = 4
c = 1
s = [[0 for _ in range(n)] for i in range(n)]

-sharp 
for i in range(n):
    for j in range(i+1):
        a, b = (i-j, j) if i%2 != 0 else (j, i-j)
        s[a][b] = c
        c += 1
-sharp         
for i in range(n, n*2):
    for j in range(1, n*2-i):
        a, b = (n-j, i-n+j) if i%2 != 0 else (i-n+j, n-j)
        s[a][b] = c
        c += 1
-sharp         
for i in s:
    print(i)

output results

[1, 3, 4, 10]
[2, 5, 9, 11]
[6, 8, 12, 15]
[7, 13, 14, 16]
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e46525-44ba9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e46525-44ba9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?