python - Using a lambda as key within builtin string sort(), hard to identify Big-O -


the following code made takes every string in y , places anagrams in list:

>>> y = ['eat','beat','sweet','tea', 'eta', 'teews', 'leet', 'tele'] >>> y.sort(key=lambda x: sorted(x)) >>> y ['beat', 'eat', 'tea', 'eta', 'leet', 'tele', 'sweet', 'teews'] 

when trying give time complexity solution, had hard time. think o(nlogn) not sure lambda doing here. because not sure going on under hood python.

can shed light on time complexity using lambda key within pythons string sort builtin?

the lambda function passed argument while sorting applied once each element iterable, , used comparaison between each others.

y.sort(key=lambda x: sorted(x)) means each element x in list y, stored(x) called , determine string has come first in final list.

as sorting indeed o(n log n), , saying list contains m strings of n average length, complexity here o(m * (n log n) + (m log m)).


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -