Max of Three

Strategy 1: Compare each to all

In [10]:
from random import randrange
In [11]:
def compare1():
    #x1, x2, x3 = eval(input("Please enter three values: "))
    x1, x2, x3 = randrange(0, 100), randrange(0, 100), randrange(0, 100)
    print("Random nums: {0} {1} {2}".format(x1, x2, x3))
    
    if x1 >= x2 and x1 >= x3:
        _max = x1
    elif x2 >= x1 and x2 >= x3:
        _max = x2
    #elif x3 >= x2 and x3 >= x1:
    else:
        _max = x3
    
    print("The largest value is", _max)
In [12]:
compare1()
Random nums: 10 27 70
The largest value is 70
In [2]:
x1 = 5
x2 = 2
x3 = 4
x1 >= x2>= x3
Out[2]:
False
In [13]:
def compare2():
    x1, x2, x3 = randrange(0, 100), randrange(0, 100), randrange(0, 100)
    print("Random nums: {0} {1} {2}".format(x1, x2, x3))
    
    if x1 >= x2:
        if x1 >= x3:
            _max = x1
        else:
            _max = x3
    else:
        if x2 >= x3:
            _max = x2
        else:
            _max = x3

    print("The largest value is", _max)
In [14]:
compare2()
Random nums: 68 69 15
The largest value is 69
In [15]:
def compare3():
    x1, x2, x3 = randrange(0, 100), randrange(0, 100), randrange(0, 100)
    print("Random nums: {0} {1} {2}".format(x1, x2, x3))
    
    _max = x1
    
    if x2 > _max:
        _max = x2
        
    if x3 > _max:
        _max = x3

    print("The largest value is", _max)
In [16]:
compare3()
Random nums: 40 56 2
The largest value is 56
In [19]:
def compare4():
    n = int(input("How many numbers are there? "))
    
    _max = randrange(0, 100)
    print("{0}".format(_max))
        
    for i in range(n-1):
        x = randrange(0, 100)
        print("{0}".format(x))
        if x > _max:
            _max = x

    print("The largest value is", _max)
In [18]:
compare4()
How many numbers are there? 10
3
49
80
94
88
13
90
44
11
41
The largest value is 94
In [20]:
compare4()
How many numbers are there? 100
81
47
38
13
63
15
47
91
43
81
50
69
91
26
36
60
61
38
29
94
1
12
32
25
22
53
75
15
46
91
96
96
68
9
10
5
10
77
96
21
5
93
25
28
42
28
87
30
60
0
95
76
79
89
41
87
56
14
8
11
39
68
91
7
2
44
73
68
16
42
55
71
65
58
49
12
56
41
16
5
55
83
43
59
42
24
79
97
88
32
92
36
9
31
53
37
23
87
91
63
The largest value is 97
In [25]:
def compare5():
    n = int(input("How many numbers are there? "))
    
    rand = []
            
    for i in range(n):
        x = randrange(0, 100)
        rand.append(x)
        print("{0}".format(x))

    _max = max(rand)
    print("The largest value is", _max)
In [26]:
compare5()
How many numbers are there? 10
13
12
37
97
94
78
66
24
15
88
The largest value is 97
In [28]:
#lst = []
#lst[0] = 134

lst = [0, 0, 0, 0]
lst[0] = 134

nums = [1,2, 3]
nums = nums + [4]
In [29]:
nums
Out[29]:
[1, 2, 3, 4]