An exploration of the multiple ways of finding the maximum of three numbers.

Question to you, which one is best?

We start with this basic setup:

```
# This is our template for a bunch of our examples below
def max_num():
input_str = input("Please enter three values (x1,x2,x3): ")
# These two lines are used instead of `eval()`
x1, x2, x3 = input_str.split(",")
x1, x2, x3 = int(x1), int(x2), int(x3)
# Missing code here...
print("The largest value is {0}".format(max_))
```

- Worst Case: 4 comparisons to determine
`x1`

,`x2`

are smaller than`x3`

. - Best Case: 2 comparisons are made to determine that
`x1`

is the largest number.

* We make the decision that `x1`

is the first number we check.

In [3]:

```
def max_num_1():
input_str = input("Please enter three values (x1,x2,x3): ")
# These two lines are used instead of `eval()`
x1, x2, x3 = input_str.split(",")
x1, x2, x3 = int(x1), int(x2), int(x3)
if x1 >= x2 and x1 >= x3:
max_ = x1
elif x2 >= x1 and x2 >= x3:
max_ = x2
else:
max_ = x3
print("The largest value is {0}".format(max_))
```

In [4]:

```
max_num_1()
```

Please enter three values (x1,x2,x3): 33,42,12 The largest value is 42

- Worst Case: 2 comparisons to determine
`x1`

,`x2`

are smaller than`x3`

. - Best Case: 2 comparisons to determine
`x1`

is the largest number.

* We make the decision that `x1`

is the first number we check.

In [5]:

```
def max_num_2():
input_str = input("Please enter three values (x1,x2,x3): ")
# These two lines are used instead of `eval()`
x1, x2, x3 = input_str.split(",")
x1, x2, x3 = int(x1), int(x2), int(x3)
if x1 >= x2:
if x1 >= x3:
max_ = x1
else:
max_ = x3
else:
# We are here because x2 is larger than x1
if x2 >= x3:
max_ = x2
else:
max_ = x3
print("The largest value is {0}".format(max_))
```

In [7]:

```
max_num_2()
```

Please enter three values (x1,x2,x3): 33,42,12 The largest value is 42

This quickly becomes an ugly and unmanageable problem. We get seriously messy code.

In [8]:

```
def max_num_2a():
input_str = input("Please enter three values (x1,x2,x3): ")
# These two lines are used instead of `eval()`
x1, x2, x3, x4 = input_str.split(",")
x1, x2, x3, x4 = int(x1), int(x2), int(x3), int(x4)
if x1 >= x2:
if x1 >= x3:
if x1 >= x4:
max_ = x1
else:
max_ = x4
else:
if x3 >= x4:
max_ = x3
else:
max_ = x4
else:
if x2 >= x3:
if x2 >= x4:
max_ = x2
else:
max_ = x4
else:
if x3 >= x4:
max_ = x3
else:
max_ = x4
print("The largest value is {0}".format(max_))
```

In [9]:

```
max_num_2a()
```

Please enter three values (x1,x2,x3): 33,42,12,99 The largest value is 99

- Worst Case: 2 comparisons to determine
`x1`

,`x2`

are smaller than`x3`

. - Best Case: 2 comparisons to determine
`x1`

is the largest number.

In [10]:

```
def max_num_3():
input_str = input("Please enter three values (x1,x2,x3): ")
# These two lines are used instead of `eval()`
x1, x2, x3 = input_str.split(",")
x1, x2, x3 = int(x1), int(x2), int(x3)
max_ = x1 # This is purely a guess, and that's OK.
if x2 > max_:
max_ = x2
if x3 > max_:
max_ = x3
print("The largest value is {0}".format(max_))
```

In [11]:

```
max_num_3()
```

Please enter three values (x1,x2,x3): 33,42,12 The largest value is 42

It now becomes easy to do and read.

In [16]:

```
def max_num_3a():
input_str = input("Please enter three values (x1,x2,x3,x4): ")
# These two lines are used instead of `eval()`
x1, x2, x3, x4 = input_str.split(",")
x1, x2, x3, x4 = int(x1), int(x2), int(x3), int(x4)
max_ = x1 # This is purely a guess, and that's OK.
if x2 > max_:
max_ = x2
if x3 > max_:
max_ = x3
if x4 > max_:
max_ = x4
print("The largest value is {0}".format(max_))
```

In [17]:

```
max_num_3a()
```

Please enter three values (x1,x2,x3,x4): 33,42,12

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-17-6c6aaa6ff7af> in <module> ----> 1 max_num_3a() <ipython-input-16-52c45919ec6e> in max_num_3a() 3 4 # These two lines are used instead of `eval()` ----> 5 x1, x2, x3, x4 = input_str.split(",") 6 x1, x2, x3, x4 = int(x1), int(x2), int(x3), int(x4) 7 ValueError: not enough values to unpack (expected 4, got 3)

In [18]:

```
max_num_3a()
```

Please enter three values (x1,x2,x3,x4): 33,42,12,99 The largest value is 99

It now becomes easy to do and read.

In [30]:

```
def max_num_3b():
n = int(input("How many numbers are there to enter? "))
max_ = float(input("Enter a number >>> "))
for i in range(n-1):
x = float(input("Enter a number >>> "))
if x > max_:
max_ = x
print("The largest value is {0}".format(max_))
```

In [29]:

```
max_num_3b()
```

In [31]:

```
def max_num_4():
input_str = input("Please enter three values (x1,x2,x3): ")
# These two lines are used instead of `eval()`
x1, x2, x3 = input_str.split(",")
x1, x2, x3 = int(x1), int(x2), int(x3)
max_ = max(x1, x2, x3)
print("The largest value is {0}".format(max_))
```

In [32]:

```
def max_num_4a():
input_str = input("Please enter three values (x1,x2,x3,..,x_n): ")
# These two lines are used instead of `eval()`
nums = input_str.split(",")
for i in range(len(nums)):
nums[i] = float(nums[i])
max_ = max(nums)
print("The largest value is {0}".format(max_))
```

In [ ]:

```
```