Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
The Python docs remind us that it's easier to ask for forgiveness than permission (although, as someone mentioned in another comment, it would be better to reduce the scope of the exception handling to
except TypeError:
).Having said that, Guido himself (at least on March 11, 2014) seems to agree with you.
Sorting is already creating new lists in memory, regardless of squaring each value. Compared to the sorted operation, squaring each value is negligible and neither increases the time or space complexity.
And generally speaking, optimizations where you can return early don't decrease the time complexity in the average case so aren't really used as a way to judge algorithms in problems like this.
In many situations it is considered Pythonic to not use if statements to check things but instead to just try it and catch on failure.
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution