Find the maximum number in a list

problem

Given a list of numbers e.g. [1, 3, 5, 6, 9, 11, 38, 40] we wish to find the maximum number and return it. The numbers are not sorted in any way and the list contains at least 1 element.

SOLUTION

A Python function will do the trick. Let’s write a small program made up from two functions. The main() one which is the entry point of the program and a function find_maximum() that the main() will call.

The find_maximum accepts an argument, the list of numbers.

The definition will be as follows:

				
					def find_maximum(numbers):
     #finds the maximum number in numbers
				
			

The find_maximum accepts the list of numbers that has at least 1 element. Now, in order to find the maximum number, one approach is to use the first element of the list as the maximum and compare it with the rest. We will use a maximum variable to keep the maximum and a for loop to do all the comparison. So the code now becomes as follows:

				
					def find_maximum(numbers):
        maximum = numbers[0] #treat first element as the maximum
        return maximum
				
			

In the case that the list is singleton (having only 1 element), then that element will be the maximum by default.

The code above stores the first element of the list in the maximum variable and simply returns it. Now, let’s add a loop to iterate through the remaining elements of the list. The remaining can be retrieved using the slice notation. If a list numbers contains 10 items and we need all but not the first one, then it can be done with:

				
					numbers[1:]
				
			

Adding it altogether:

				
					def find_maximum(numbers):
        maximum = numbers[0]
        for number in numbers[1:]:
            if number > maximum:
                maximum = number
        return maximum
				
			
The above code iterates through the elements and compares each one with what was stored in maximum. If the current number is greater then the maximum gets updated and so on.
running
Finally, let’s put a main function that will call the find_maximum() with some real example.
				
					def find_maximum(numbers):
    maximum = numbers[0]
    for number in numbers[1:]:
        if number > maximum:
            maximum = number
    return maximum

def main():
    print(find_maximum([2,5,3]))

if __name__ == "__main__":
    main()

				
			

In the above code, we’re calling the function with the list [2,5,3]. The main function will print the result of find_maximum([2,5,3]). That was a small shortcut. To write that simpler, we can introduce a new variable to hold the function’s result as follows:

				
					def main():
        maximum = find_maximum([2,5,3])
        print(maximum)
				
			
output

And running it will give us 5 as expected!

conclusion

Feel free to download the complete solution and experiment with adding negative numbers, floats, etc to the list and inspect the output.

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x