Sort & Join the longest k arrays

This function concatenate longest k number of arrays to a single string.

def longest_kconsec(strarr, k):
   if (k>0&k<=len(strarr)):
        sort = sorted(set(strarr),key = lambda x:len(x), reverse = True)[:k]
        return ''.join(sort)
   else:
        return ''

Executing…

l = ['r','d','wdfe','ff','gfg','wfdew','d','ff']
longest_kconsec(l, 2)

returns…
'wfdewwdfe'

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s