We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Try this
def bigSorting(unsorted):
return sorted(unsorted, key=lambda x: (len(x), x))
This solution sorts the strings based on two criteria:
The length of the string (shorter strings come first)
The lexicographical order of the string itself
It avoids converting strings to integers, which can be slow for very large numbers.
It handles numbers of different lengths correctly without conversion.
It's faster for comparing very large numbers, as it stops comparing as soon as it finds a difference.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Big Sorting
You are viewing a single comment's thread. Return to all comments →
Try this def bigSorting(unsorted): return sorted(unsorted, key=lambda x: (len(x), x))
This solution sorts the strings based on two criteria: The length of the string (shorter strings come first) The lexicographical order of the string itself
It avoids converting strings to integers, which can be slow for very large numbers. It handles numbers of different lengths correctly without conversion. It's faster for comparing very large numbers, as it stops comparing as soon as it finds a difference.