0
I made the following code that generates the matrix with random values as the dimensions are passed, and brings the result of the lowest value and its position and the highest value and its position. Instead of passing the dimensions of the matrix and it being generated, I would like to type each matrix value, separated by blank spaces. If the first line is empty, display the message that it is empty. Entering the values of the matrix, display the final typed matrix and its respective smallest and largest value and its positions.
Matrix size is not set, if it is 4x4 for example. You enter the amount you want.
Currently the code is like this:
def generate(numLins, numCols, mn, mx):
from random import randint
values = [None] * numLins
for i in range(numLins):
values[i] = [0] * numCols
for j in range(numCols):
values[i][j] = randint(mn, mx)
return values
def reveal(values, linMn, linMx, colMn, colMx):
for i in range(linMn, linMx):
for j in range(colMn, colMx):
print(values[i][j], end=" ")
print()
print()
return None
def localizaCelulaComMaiorValor(vals):
resp = (0, 0)
for lin in range(len(vals)):
for col in range(len(vals[lin])):
if vals[lin][col] > vals[resp[0]][resp[1]]:
resp = (lin, col)
return resp
def localizaCelulaComMenorValor(vals):
resp = (0, 0)
for lin in range(len(vals)):
for col in range(len(vals[lin])):
if vals[lin][col] < vals[resp[0]][resp[1]]:
resp = (lin, col)
return resp
print("Dimensões da Matriz:")
dimensoes = input().split()
numLins = int(dimensoes[0])
numCols = int(dimensoes[1])
valores = generate(numLins, numCols, 10, 99)
reveal(valores, 0, numLins, 0, numCols)
ondeMaior = localizaCelulaComMaiorValor(valores)
print("Maior Valor:", valores[ondeMaior[0]][ondeMaior[1]], "na posição: (", ondeMaior[0], ",", ondeMaior[1], ")")
ondeMenor = localizaCelulaComMenorValor(valores)
print("Menor Valor:", valores[ondeMenor[0]][ondeMenor[1]], "na posição: (",ondeMenor[0],",",ondeMenor[1],")" )
This answer uses the fixed matrix size. In my case, it is not
– MSSantana
In this case, just place the dimension inserted by the user at the limits of the iteration. In the given example, it was a 4x4 matrix. In your case, it will be a matrix dimensos[0]xdimensoes[1].
– Rafael Barros
Where in the possible duplicate code is range(4), it will be range(int(dimensions[0])) or range(int(dimensions[1])).
– Rafael Barros
I tried to run the code here and it’s not working. Being a beginner, it might be a fault of mine too.
– MSSantana
Take the example of the other post and try to implement it, because it is much less verbiage and has already been solved. Then show us the mistakes here so we can help you. :)
– Rafael Barros