1) Read a 10 x 10 matrix and write the location (row and column) of the highest value

Asked

Viewed 436 times

0

CHALLENGE OF THE QUESTION 1 | Now, do not forget to inform which is the position of this higher value.

There’s no way I can show the position of the highest value:

from random import randint

matriz_10x10 = []

for linha in range(10):
    linha = []

    for coluna in range(10):
        linha.append(randint(0, 10000))

    matriz_10x10.append(linha)

for linha_matriz in matriz_10x10:
    print(linha_matriz)

This returns me to the following list:

[9084, 3990, 7211, 8670, 7057, 1310, 172, 6029, 7080, 2736]
[1039, 5584, 94, 428, 2429, 8719, 7838, 9331, 9274, 3163]
[166, 664, 5928, 656, 4576, 2585, 9654, 3071, 530, 9457]
[8563, 969, 9464, 2161, 3446, 718, 5524, 4919, 8249, 2624]
[2436, 1633, 1146, 2718, 4335, 5981, 772, 1345, 2126, 888]
[9860, 8196, 7874, 2661, 3504, 9538, 4872, 7993, 8629, 6598]
[8, 527, 5426, 4970, 8553, 5912, 2510, 396, 1304, 8640]
[4661, 1554, 4380, 2625, 6098, 4635, 9876, 503, 5484, 2131]
[8612, 1953, 2372, 6697, 4973, 7691, 8735, 4418, 7584, 3881]
[3561, 8265, 9219, 5248, 1018, 9481, 51, 8268, 3733, 9293]

maior_valor = 0

for linha in matriz_10x10:
    for coluna in linha:
        if maior_valor < coluna:
            maior_valor = coluna

print(maior_valor)

Retorna: 9876

Now comes the part that shows the position of the value 9876 in the list... Does anyone have any idea?

  • You need to create two variables, row and column, which update whenever the highest value is found.

  • I tried to do this, but I am beginner in python, these problems were passed on to me as a challenge. .

2 answers

1


The only thing you need to do is get the value position with the method index and then store it to a variable.

See the example below where I create a tuple that stores the highest value and its position.

element = (0, )

for line in matriz:
    for value in line:
        if element[0] < value:
            x = line.index(value)
            y = matriz.index(line)
            element = (value, y, x)

You can also use the range in the repeating structure for to obtain the indexes without using the method.

element = (0, )

for line in range(len(matriz)):
    for column in range(len(matriz[line])):

        value = matriz[line][column]

        if element[0] < value:
            element = (value, line, column)

1

Since you want both the largest element and its position, one option is to use enumerate, which allows the elements and their indexes to be iterated at the same time.

Then just hold the position whenever a larger element is found:

maior_valor = -1

for i, linha in enumerate(matriz_10x10):
    for j, coluna in enumerate(linha):
        if maior_valor < coluna:
            maior_valor = coluna
            posicao_maior = (i, j)

print(maior_valor)
print(posicao_maior)

Detail that the highest value starts with a value that is less than all possible values in the list. This avoids the rare - but possible - case in which all elements are zero, because if maior_valor started with zero, would never enter the if and the position would not be set (this is a case that the another answer did not treat, since it starts the largest with zero - even though it is rare and unlikely that all values are zero, it is still possible, and it is better to have something that always works, even more if the solution is as simple as changing the initial value).

The other solution with index is bad because index goes through the list from the beginning to find the index, which is not only inefficient, since you are already in the middle of a loop scrolling through the list, then to scroll through it again to search for the index if you can already have it directly using enumerate? (by going through the same list several times without need, you are using the "algorithm" of Shlemiel the Painter)

Of course for small lists, and running a few times, the difference will be insignificant, but it is important to pay attention to these details. In real systems, with lots of data and running over and over, things like this can make all the difference.

Browser other questions tagged

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