Minimum

In the spirit of finals, I thought I’d post some algorithms in Python. This one goes through a list and finds the smallest element. It does this by assuming the first element is the smallest, then it goes through the rest of the list and if it sees a smaller element, it marks that one as the smallest. Pretty simple, huh?

def minimum(lst):
    smallest = lst[0]
    for x in lst:
        if x < smallest:
            smallest = x
    return smallest

Post a Comment

Your email is never published nor shared.