How to create a function to find the largest value in a list?

-3

I'm not able to insert this algorithm into a function

max_trip = 0

for val in trip_duration_list:
    if int(val) > max_trip:
        max_trip = int(val)
    
asked by anonymous 06.12.2018 / 01:26

3 answers

2
def maior_valor(lista):
    try:
        if len(lista) == 0:
            return None

        maior = lista[0]

        for valor in lista:
            if valor > maior:
                maior = valor

        return maior
    except TypeError:
        return lista

Considerations:

  • Due to the dynamic typing of Python, there is no guarantee that the parameter received by the function is of the expected type. The try/catch structure is used to control the program flow if the input is not iterable. When it is, it iterates the object by checking its highest value; when it is not, returns the element itself;

  • If the input parameter has size 0, it will return None because there are no values to be compared;

  • The initial value of maior will always be the first element in the list, because this circumvents the problem of starting at 0 but the input list has only negative elements. The highest value in [-1, -2, -3] can not be 0.

  • Limitations:

  • Comparing value by value, the list is not allowed to have values of different types, such as [1, '2', (3,)] . This even makes sense, as a way of comparing different types is not natively predicted. If you consider the fact that, generally, but not necessarily, lists are homogeneous, this is no longer a limitation, but for tuples, which are often heterogeneous, you would continue with the problem.

  • The behavior of the function may be strange when the input is a string , because it will be iterated and the last character will be returned in alphabetical order. That is, maior_valor('anderson') would return 's' ;

  • Usage:

    assert maior_valor([1, 2, 3, 4]) == 4
    assert maior_valor([-1, -2, -3]) == -1
    assert maior_valor('anderson') == 's'
    assert maior_valor(5) == 5
    assert maior_valor([]) is None
    assert maior_valor('') is None
    assert maior_valor([1, '2', (3,)]) == [1, '2', (3,)]
    

    Additional readings:

    06.12.2018 / 11:44
    1

    You can use max() to do this. Example:

    list_1, list_2 = [123, 'xyz', 'Rafinha', 'abc'], [325, 600, 199]
    print "Max value: ", max(list_1)
    print "Max value: ", max(list_2)
    
    #Max value:  Rafinha
    #Max value:  600
    

    If it is a necessity to create a method for this you just need to include this logic in a method.

        
    06.12.2018 / 01:38
    0

    First you need to create a function and say that it receives a list as a parameter:

    def verificaMaior(trip_duration_list):
    

    Then you need to set an initial value to the variable you want to compare:

    max_trip = trip_duration_list[0]
    

    Just after these two basic points you have to go through your list checking if there is any number greater than your comparison variable:

    for val in trip_duration_list:
        if max_trip < val:
            max_trip = int(val)
    

    This is only to return your variable with updated value (the updated value will only be set if there is a larger number in your list):

    return max_trip
    

    Complete code:

    def verificaMaior(trip_duration_list):
      max_trip = trip_duration_list[0];
      for val in trip_duration_list:
        if max_trip < val :
          max_trip = int(val)
      return max_trip
    
    print(verificaMaior([1, 2, 3, 4, 10]))
    

    The final print is to show the return of your function in the console.

        
    06.12.2018 / 01:42