Select the two largest values of a vector and add them?

0
atletas = []

while True:

    nome = input("Nome: ")

    if not nome: break

    saltos = []
    for i in range(3):
        salto = float(input("Distância {}: ".format(i+1)))
        saltos.append(salto)

    atletas.append({
        "nome": nome,
        "saltos": saltos
    })

for atleta in atletas:
    print("Nome:", atleta["nome"])
    print("Saltos:", atleta["saltos"])
    print("Média:", sum(atleta["saltos"])/3)
    
asked by anonymous 12.06.2018 / 23:24

1 answer

1

You can sort your vector in descending order using the sort() method and passing the reverse=True argument.

Once sorted in descending order, you can use the sum() function to add only the first two elements of the vector, see:

vetor = [ 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 ]
vetor.sort(reverse=True)
print sum( vetor[:2] )

Output:

17
    
13.06.2018 / 02:30