-
Notifications
You must be signed in to change notification settings - Fork 23
/
find_max.py
60 lines (47 loc) · 1.64 KB
/
find_max.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
################################################################################
#
# Program: Find The Maximum Number In A List WITHOUT Using max().
#
# Description: How to find the largest value in a list in Python without using
# the built-in max() function.
#
# YouTube Lesson: https://www.youtube.com/watch?v=RNHhgJcDjI8
#
# Author: Kevin Browne @ https://portfoliocourses.com
#
################################################################################
# Returns the maximum value in the list supplied as an argument
def find_max(list):
# Assume that the first element in the list is the maximum value in the list
max = list[0]
# Go through the remaining elements in the list, and whenever a value is
# encountered that is greater than the current max value, make this value
# the new max value.
for number in list:
if (number > max):
max = number
# By the end of the above loop max will contain the largest value in the
# list and we return that value from the function
return max
# An example list where 9 is the max value
list = [3,7,5,8,4,9]
# Call the find_max() function with the list as an argument, store the max value
# returned into max
max = find_max(list)
# Print out the max value that was found in the list
print("Max:", max)
# Python's built-in max() function will also find the maximum value...
#
# print(max(list))
# We don't need to put the algorithm to find the maximum number in a list inside
# a function, we could just write a simple program like this instead:
#
#
# list = [3,7,5,8,4,9]
#
# max = list[0]
# for data in list:
# if (data > max):
# max = data
#
# print("Max:", max)