Python – Sort Strings by Maximum ASCII value
Last Updated :
08 Feb, 2024
Given strings list, perform sort by Maximum Character in String.
Input : test_list = [“geeksforgeeks”, “is”, “best”, “cs”]
Output : [“geeksforgeeks”, “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
def get_max(sub):
return ord ( max (sub))
test_list = [ "geeksforgeeks" , "is" , "best" , "for" , "cs" ]
print ( "The original list is : " + str (test_list))
test_list.sort(key = get_max)
print ( "Sorted List : " + str (test_list))
|
Output
The original list is : ['geeksforgeeks', 'is', 'best', 'for', 'cs']
Sorted List : ['for', 'geeksforgeeks', '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
test_list = [ "geeksforgeeks" , "is" , "best" , "for" , "cs" ]
print ( "The original list is : " + str (test_list))
res = sorted (test_list, key = lambda sub: ord ( max (sub)))
print ( "Sorted List : " + str (res))
|
Output
The original list is : ['geeksforgeeks', 'is', 'best', 'for', 'cs']
Sorted List : ['for', 'geeksforgeeks', '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 = [ "geeksforgeeks" , "is" , "best" , "cs" ]
sorted_list = heapq.nlargest( len (test_list), test_list, key = lambda s: max ( map ( ord , s)))
print (sorted_list)
|
Output
['best', 'geeksforgeeks', '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
test_list = [ "geeksforgeeks" , "is" , "best" , "for" , "cs" ]
def max_char_ascii(s):
return ord ( max (s))
lst = [(s, max_char_ascii(s)) for s in test_list]
res = sorted (lst, key = lambda x: x[ 1 ])
res = [t[ 0 ] for t in res]
print ( "Sorted List : " + str (res))
|
Output
Sorted List : ['for', 'geeksforgeeks', '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)
Share your thoughts in the comments
Please Login to comment...