Why -99999 is included in the function to obtain the maximum value

Asked 6 days ago, Updated 6 days ago, 1 views

def our_max(numbers):
    result = -9999
    for number in numbers:
        if result<number:
            result = number
    return result
print(our_max([1, 2, 10, 9, 3, 7, 0, 99, 27, 85]))

python

2022-09-20 14:44

1 Answers

The purpose of the code appears to be to obtain the maximum value among the elements in the list [1, 2, 10, 9, 3, 7, 0, 99, 27, 85].

If you look at the contents of the code, you are repeating the size comparison of each element and the reference value.

You're going to need an arbitrary number to compare numbers. You set the result = -9999 here. To be honest, there is no problem with the example code even if it is -1.

You can use the max command without having to use these functions, but... It's written for something.

To be honest, it's right to ask the person who wrote the code directly rather than the others.


2022-09-20 14:44

If you have any answers or tips


© 2022 pinfo. All rights reserved.