How do I find the largest number read and the smallest number read?

Asked

Viewed 74 times

-1

How do I find the largest number read and the smallest number read?
I tried the max and the min and it didn’t work.

arquivo = open('notas_alunos.txt', 'r')

lista_notas = []

conteudo = arquivo.readline()

linha = conteudo[:-1].split(';')
lista_notas.append(linha)

while conteudo:
   conteudo = arquivo.readline()
   linha = conteudo[:-1].split(';')
   lista_notas.append(linha)


arquivo.close()


lista_notas.pop()

for elemento in range(0, len(lista_notas)):
    matricula = lista_notas[elemento][0]  
    nota_1    = float(lista_notas[elemento][1])    
    nota_2    = float(lista_notas[elemento][2])  
    nota_3    = float(lista_notas[elemento][3])  
    nota_4    = float(lista_notas[elemento][4])             
    media     = (nota_1 + nota_2 + nota_3 + nota_4)/4  
    print('{0} {1:.2f} {2:.2f} {2:.2f} {3:.2f} {4:.2f}'.format(matricula,nota_1,nota_2,nota_3,nota_4,media)) 
  • 1

    Always search the official documentation. See the topic on data structure: https://docs.python.org/2/tutorial/datastructures.html and note that lists do not contain "max" and "min". You can sort with "Sort", for example: list.Sort() and the Boolean parameter "Reverse" will define whether the sort will be ascending or descending and you can also define a key for your sort, for example: list.Sort(Reverse=True, key=orderSame) or list.Sort(Reverse=True, key=lambda value: value['value'])

2 answers

0

Hello,

if you were able to use the method suggested by Lgregianin just take the first or last element of the list, depending on the value of the reversed parameter within the Sort method.

If reversed is true, the list will be ordered from the highest value, therefore at index 0, to the lowest value, at Indice -1. Otherwise just reverse the positions.

Anyway, it’s not entirely clear if you want to know the highest and lowest read values in total or want the highest/lowest average, etc. If you can detail further the problem we can help more precisely.

  • I understood, but how would I find the highest and lowest value ?

  • It worked, folks, thank you very much

0

Well, there are some ways to find the lowest and highest value of a set of numbers. Ideally, you understand logic and not just copy a ready-made answer. The simplest way I consider it is to put all values to be analyzed in an array, scan it and make the assignment based on the conditional array[posição] < menorValor ? or array[posição] > maiorValor ?

menor = arr[0];
maior = arr[0];

for(int i=0; i < arr.tamanho; i++ ){
    if(arr[i] > maior) 
        maior = arr[i];
    if(arr[i] < menor)
        menor = arr[i];             
}

Each time the scan finds a value less (or greater) than the value of the current variable, the value will be replaced by the same. (Try to apply logic to your language)

Browser other questions tagged

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