-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathfind_min.py
More file actions
55 lines (45 loc) · 1.6 KB
/
find_min.py
File metadata and controls
55 lines (45 loc) · 1.6 KB
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
################################################################################
#
# Program: Find The Minimum Number In A List WITHOUT Using min().
#
# Description: How to find the smallest value in a list in Python without using
# the built-in min() function.
#
# YouTube Lesson: https://www.youtube.com/watch?v=Qe8Q8dQRIJY
#
# Author: Kevin Browne @ https://portfoliocourses.com
#
################################################################################
# Returns the minimum value in the list supplied as an argument
def find_min(list):
# Assume that the first element in the list is the minimum value in the list
min = list[0]
# Go through the remaining elements in the list, and whenever a value is
# encountered that is less than the current min value, make this value
# the new min value.
for number in list:
if (number < min):
min = number
# By the end of the above loop min will contain the smallest value in the
# list and we return that value from the function
return min
# An example list where 2 is the min value
list = [8,9,4,5,2,6]
# Call the find_min() function with the example list as an argument and output
# the min value returned from the function
print( find_min(list) )
# Python's built-in min() function will also find the maximum value...
#
# print(min(list))
# We don't need to put the algorithm to find the minimum number in a list inside
# a function, we could just write a simple program like this instead:
#
#
# list = [8,9,4,5,2,6]
#
# min = list[0]
# for data in list:
# if (data > max):
# min = data
#
# print(min)