Monday, June 24, 2019

IslandT: Find the smallest number within a list with python

In this example, we will create a python function which will take in a list of numbers and then return the smallest value. The solution to this problem is first to create a place holder for the first number within the list, then compares that number with other numbers within the same list in the loop. If the program found a number which is smaller than the one in the place holder, then the smaller number will be assigned to that place holder.

def find_smallest_int(arr):
    smallest = None
    for elem in arr:
        if(smallest == None or smallest > elem):
            smallest = elem
            
    return smallest

If you have a better solution then kindly leave your comment below this post.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...