DEV Community

Discussion on: [Python] Find the Largest Number From a Array.

Collapse
 
patricktingen profile image
Patrick Tingen

Why reinvent a wheel when you already have one?

def largest(arr):  
    arr.sort(reverse=True)
    if len(arr) > 0:
        return arr[0]
    else:
        return 0

numbers = [1, 3, 4, 2]
print(largest(numbers))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jayjeckel profile image
Jay Jeckel

The passed array doesn't need to be changed and shouldn't be changed by a method that only needs to look at its contents. You've added a side effect to a method that shouldn't have any side effects. Other than a few nitpicks, the article's example of the method is the correct way of doing it.

Collapse
 
hunter profile image
Hunter Henrichsen • Edited
numbers = [1, 3, 4, 2]
print(max(numbers))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
eshu profile image
Eshu

What the complexity of sort? :)