Implement a function that takes as input three variables, and returns the largest of the three. Do this without using the Python max()
function!
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.
def max_of_three(a,b,c): | |
max_3=0 | |
if a>b: | |
#max_3=a | |
if a>c: | |
max_3=c | |
else: | |
max_3=a | |
else: | |
if b>c: | |
max_3=b | |
else: | |
max_3=c | |
return max_3 |
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.
#!/usr/bin/env python | |
import sys | |
if len(sys.argv) < 4: | |
print 'Usage <value1> <value2> <value3>' | |
sys.exit ( 1 ) | |
arg1 = sys.argv[1] | |
arg2 = sys.argv[2] | |
arg3 = sys.argv[3] | |
def maxfunction(a,b,c): | |
if (a > b) and (a > c): | |
print 'Max value is :',a | |
elif (b > a) and (b > c): | |
print 'Max value is :',b | |
elif (c > a) and (c > b): | |
print 'Max value is :',c | |
maxfunction(arg1,arg2,arg3) |
Which one is your favorite? Why?
Happy hacking!