Python homework.
Write your own versions of the Python built-in functions min and max. They should take a list as an argument and return the minimum or maximum element. Hint: Pick the first element as the minimum (maximum) and then loop through the elements to find a smaller (larger) element. Each time you find a smaller (larger) element, update your minimum (maximum). Ensure you are supplied valid input i.e list of only numbers
def minimum(lst): m = lst[0] i = 0 for x in lst: if x < m: m = x i += 1 return m def maximum(lst): m = lst[0] i = 0 for x in lst: if x > m: m = x i += 1 return m # Testing print(maximum([4,2,5,1,3])) print(minimum([4,2,5,1,3]))
Get Answers For Free
Most questions answered within 1 hours.