Practice Python

Beginner Python exercises

16 July 2016

Max Of Three Solutions

Exercise 28

Implement a function that takes as input three variables, and returns the largest of the three. Do this without using the Python max() function!

Sample solutions

There are many ways to answer this question, ranging from simple to complex. Here are a few reader-submitted answers!

This first example solution uses a series of if statements and comparisons to find the largest of 3 elements.

Another solution is a little bit less verbose, taking 3 numbers as an input, making them into a list, sorting them, and then reading off the largest element.

This last solution uses a more compact series of if statement comparisons to cover all cases of 3 elements.

Which one is your favorite? Why?

Happy hacking!


comments powered by Disqus