How could I find the minimum value without using min( )?


#1

Question

How could I find the minimum value without using min( )?

Answer

If you wanted to implement your own min() function, you could definitely do so! We haven’t quite learned all the parts of it yet, but if you’re up for a challenge, try to understand the basics of what is happening below:

def minimum(list):
  current_min = list[0]  # Start by assuming the first number is smallest
  for num in list:       # Loop through each number in the list
    if num < current_min:
      current_min = num  # Update current_min if current number is less
  return current_min

print minimum([1,2,3,-1])  # -1

You could make a very small modification to the above code to find max, as well!