You are viewing a single comment's thread. Return to all comments →
We need to sort by len and then lexigraphically using lambda x: (len(x), x). Refer also below comments by @spelvin and @agrigorcea.
len
lambda x: (len(x), x)
Here is len vs int using timeit on test case 4
int
import timeit with open(r"~/big_sort_input04.txt") as file: n = int(file.readline().strip()) unsorted = [] for _ in range(n): unsorted.append(file.readline().strip()) def bigSortinglen(unsorted): return sorted(unsorted, key=lambda x: (len(x), x)) def bigSortingint(unsorted): return sorted(unsorted, key=lambda x: int(x)) print( "bigSorting - len:", timeit.timeit("bigSortinglen(unsorted)", globals=globals(), number=1), ) print( "bigSorting - int:", timeit.timeit("bigSortingint(unsorted)", globals=globals(), number=1), )
And the results...
bigSorting - len: 0.007199285000751843 bigSorting - int: 3.989389060998292
Len is 550 times faster!!!
I had larger timeit numbers but got bored waiting. I'm surprised int passed all the tests!
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 →
We need to sort by
len
and then lexigraphically usinglambda x: (len(x), x)
. Refer also below comments by @spelvin and @agrigorcea.Here is
len
vsint
using timeit on test case 4And the results...
Len is 550 times faster!!!
I had larger timeit numbers but got bored waiting. I'm surprised
int
passed all the tests!