Given strings list, perform sort by Maximum Character in String.
Input : test_list = [“neveropen”, “is”, “best”, “cs”]
Output : [“neveropen”, “is”, “cs”, “best”]
Explanation : s = s = s < t, sorted by maximum character.Input : test_list = [“apple”, “is”, “fruit”]
Output : [“apple”, “is”, “fruit”]
Explanation : p < s < t, hence order retained after sorting by max. character.
Method #1 : Using sort() + max()
In this, sorting is performed using sort() and max() is used to get maximum character from Strings.
Python3
# Python3 code to demonstrate working of # Sort Strings by Maximum Character # Using sort() + max() # get maximum character fnc. def get_max(sub): # returns maximum character return ord ( max (sub)) # initializing list test_list = [ "neveropen" , "is" , "best" , "for" , "cs" ] # printing original lists print ( "The original list is : " + str (test_list)) # performing sorting test_list.sort(key = get_max) # printing result print ( "Sorted List : " + str (test_list)) |
The original list is : ['neveropen', 'is', 'best', 'for', 'cs'] Sorted List : ['for', 'neveropen', 'is', 'cs', 'best']
Method #2 : Using sorted() + lambda + max()
In this, we perform task of sorting using sorted(), lambda and max() are used to input logic of getting maximum character.
Python3
# Python3 code to demonstrate working of # Sort Strings by Maximum Character # Using sorted() + lambda + max() # initializing list test_list = [ "neveropen" , "is" , "best" , "for" , "cs" ] # printing original lists print ( "The original list is : " + str (test_list)) # performing sorting using sorted() # lambda function provides logic res = sorted (test_list, key = lambda sub: ord ( max (sub))) # printing result print ( "Sorted List : " + str (res)) |
The original list is : ['neveropen', 'is', 'best', 'for', 'cs'] Sorted List : ['for', 'neveropen', 'is', 'cs', 'best']
Time Complexity: O(n) -> built-in functions like max takes O(n)
Auxiliary Space: O(n)
Method 3: Using heapq.nlargest():
In this approach, we can use the heapq.nlargest() function to find the n largest elements of a list based on a given key function. We can pass k=len(test_list) to get all the elements of the list, and use a lambda function to return the maximum ASCII value of each string.
Python3
import heapq test_list = [ "neveropen" , "is" , "best" , "cs" ] sorted_list = heapq.nlargest( len (test_list), test_list, key = lambda s: max ( map ( ord , s))) print (sorted_list) # Output: ['neveropen', 'is', 'cs', 'best'] |
['best', 'neveropen', 'is', 'cs']
Time Complexity: O(nmlog(k)), where n is the number of strings in the list, m is the length of the longest string, and k is the number of largest elements to be found. In this case, k is equal to len(test_list), so the time complexity is O(nmlog(n)).
Auxiliary Space: O(k), where k is the number of largest elements to be found. In this case, k is equal to len(test_list), so the space complexity is O(n).
Method #4: Using a custom function with list comprehension
Python3
# Python3 code to demonstrate working of # Sort Strings by Maximum Character # Using a custom function with list comprehension # initializing list test_list = [ "neveropen" , "is" , "best" , "for" , "cs" ] # defining custom function to get maximum character ASCII value def max_char_ascii(s): return ord ( max (s)) # using list comprehension to create list of tuples lst = [(s, max_char_ascii(s)) for s in test_list] # sorting list of tuples based on second element (maximum character ASCII value) res = sorted (lst, key = lambda x: x[ 1 ]) # extracting first element of each tuple (original string) res = [t[ 0 ] for t in res] # printing result print ( "Sorted List : " + str (res)) |
Sorted List : ['for', 'neveropen', 'is', 'cs', 'best']
Time complexity: O(n log n) (due to the use of the sorted() function)
Auxiliary space: O(n) (for the lst list of tuples)