Problems with Matrices - Python 3

Asked

Viewed 98 times

-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?

1 answer

0


The total sum is simple:

somaTotal = 0
    for i in range(len(matriz)):
        for j in range(len(matriz)):
            somaTotal += matriz[i][j]
    print('somaTotal: ',somaTotal)
print('########################')

For the main diagonal note that the indexes are equal, for values that belong to it

#soma diagonal principal
somaDiaPrincipal = 0
for i in range(len(matriz)):
    for j in range(len(matriz)):
        if(i == j):
           somaDiaPrincipal += matriz[i][j]
print('somaDiaPrincipal: ',somaDiaPrincipal)
print('########################')

The secondary diagonal has the sum of its indices equal to nine (9)

somaDiaSecundaria = 0
for i in range(len(matriz)):
    for j in range(len(matriz)):
        if((i+j) == 9):
            somaDiaSecundaria += matriz[i][j]
print('somaDiaSecundaria: ',somaDiaSecundaria)

Note that the value of j for the same column is always equal, if for example you want to add column 4 put j == 4

#soma coluna central
somaColunaCentro = 0
for i in range(len(matriz)):
    for j in range(len(matriz)):
        if(j == 4):
            somaColunaCentro += matriz[i][j]
print('somaColunaCentro: ',somaColunaCentro)
  • Can you answer question 1?

  • Traverse the entire matrix and compare each value with X, if they are equal then X is in the matrix

  • That’s right, see if it’s indentation. You don’t need if

  • It was just the position of the code

  • the print was in the wrong place

  • @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 and j == (len(matriz) // 2) - 1 for example). And for the total sum, another alternative is sum(sum(linha) for linha in matriz)

  • This would make code more reusable for any matrix of the format (N x N)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.