#!/usr/bin/env python
# coding: utf-8
# # Max of Three
# 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:
# ```python
# # 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_))
# ```
# # Strategy #1: Compare Each Number to All
#
# * 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()
# ## Strategy #2: Decision Tree
#
# * 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()
# ### What if you wanted to compare 4 or more numbers?
# 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()
# ## Strategy #3 : Sequential Programming
#
# * 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()
# ### What if you wanted to compare 4 or more numbers with this sequential method?
#
# 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()
# In[18]:
max_num_3a()
# ### What if you wanted to compare $n$ numbers with this sequential method? For example, 100 numbers?
#
# 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()
# ## Strategy #4: Use Python!
# 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[ ]: