usr bin env python3 from random import randint def get_matrix_fixed re

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
from random import randint
def get_matrix_fixed():
return [
[9, 13, 5, 2],
[1, 11, 7, 6],
[3, 7, 4, 1],
[6, 0, 7, 10]
]
def get_matrix_random(size=5, value_min=0, value_max=100):
m = []
for _ in range(size):
row = [
randint(value_min, value_max)
for _ in range(size)
]
m.append(row)
return m
def print_matrix_normal(matrix):
for row in matrix:
values = [
str(item) for item in row
]
print('; '.join(values))
def print_matrix_diagonal(matrix):
if not matrix or not matrix[0]:
return
size = len(matrix) - 1
i = size
while i >= 0 - size:
if i >= 0:
y = 0
x = i
else:
y = abs(i)
x = 0
values = []
while y <= size and x <= size:
values.append(str(matrix[y][x]))
y += 1
x += 1
print('; '.join(values))
i -= 1
# matrix = get_matrix_fixed()
matrix = get_matrix_random()
print_matrix_normal(matrix)
print()
print_matrix_diagonal(matrix)