I have been asked to find the min and max of this list without using the min and max function in Python. I am not sure how to do that. Please help.
AList = 1,2,3,4
I have been asked to find the min and max of this list without using the min and max function in Python. I am not sure how to do that. Please help.
AList = 1,2,3,4
One way would be using a for loop:
def get_min_max(numbers):
if not all([type(number) in [int, float] for number in numbers]):
raise TypeError('All items in numbers must be int or float.')
min = None
max = None
for number in numbers:
min = number if min is None or number < min else min
max = number if max is None or number > max else max
return min, max
As @figbeam suggested, you can sort and retrieve the first and final value as such:
AList = [1,2,3,4]
sorted_list = sorted(AList)
minValue = sorted_list[0]
maxValue = sorted_list[-1]
print (minValue)
print (maxValue)
There are additional ways in case that example doesn't do it for you (such as making a copy of the list, then iterating through and reassigning the min (or max) based on the next entry.