-3
1 - Read a matrix D 5 x 5 (consider that duplicate values will not be reported). Then read an X number and write a message indicating whether the value of X exists or NOT in the matrix.
2 - DESAFIO | Produces a 10 x 10 matrix with random values. The matrix cannot have repeated values.
Then present:
a) the result of the sum of all matrix values;
b) the result of the sum of the main diagonal values;
c) the result of the sum of the values of the secondary diagonal;
d) the result of the sum of the central column;
So far, I have the following code:
from random import randint
matriz = []
for linha in range(10):
linha = []
for coluna in range(10):
linha.append(randint(0, 10000))
matriz.append(linha)
for linha_matriz in matriz:
print(linha_matriz)
Return :
[710, 4334, 3483, 5554, 93, 3776, 7272, 434, 8028, 7264]
[7909, 4394, 2181, 595, 204, 5274, 9520, 931, 1074, 7914]
[537, 2488, 221, 2982, 2077, 4088, 4948, 7703, 6901, 9055]
[5559, 7666, 7520, 26, 7479, 3188, 3179, 9080, 2998, 1152]
[1319, 565, 3659, 3863, 5273, 4287, 2816, 3573, 7305, 5651]
[8688, 2991, 5008, 2218, 7331, 569, 930, 4206, 1547, 22]
[2180, 7008, 3027, 597, 9550, 440, 6826, 9153, 7700, 2889]
[4929, 5792, 8233, 2604, 3764, 7251, 7469, 9888, 3777, 3286]
[4535, 5478, 4723, 872, 5162, 4547, 1570, 5542, 6809, 6064]
[3936, 2606, 1269, 7249, 1156, 6134, 8334, 5816, 1425, 6467]
How to solve the problems of the issues: a,b,c and d?
Can you answer question 1?
– Dark_Lord
Traverse the entire matrix and compare each value with X, if they are equal then X is in the matrix
– Bernardo Lopes
That’s right, see if it’s indentation. You don’t need if
– Bernardo Lopes
It was just the position of the code
– Dark_Lord
the print was in the wrong place
– Dark_Lord
@Dark_lord and Bernardo, for the last 2 solutions (secondary diagonal and central column), it is better to use matrix size instead of fixed values (
i + j == len(matriz) - 1
andj == (len(matriz) // 2) - 1
for example). And for the total sum, another alternative issum(sum(linha) for linha in matriz)
– hkotsubo
This would make code more reusable for any matrix of the format (N x N)
– Bernardo Lopes