In: Computer Science
In Rars RIsc V
Write a program that lets you enter a string.
Maximum length 50.Then, perform the sorting algorithm sort it in regards of the ascii values.
Print the string BEFORE and AFTER the sort in separate rows.
Written these code /program in python:
•write program for string of maximum length 50.and also the output i.e. without sorting (before sort)
• sorting algorithm sort in regards of ascii value.
•print a string after sort in separate rows.
#Python code to demonstrate the working of string
#Working of sort string by maximum length of the string is 50G
#GetMaximum char fnc.
def get_max(sub):
#Return Maximum character
return ord ( max ( sub ))
#initializing list
test_list = [ " geeksforgeeks " , "is" , " best", "for ","cs" ]
#printing original list
print (" the original list is:" + str (test_list))
#printing result
print (" the list is:" + strstr(test_list))
output: (before the sorting)
the original list is : ["geeksforgeeks" , "is", "best" ,"for","cs" ]
The sorting algorithm sort It in regards of ascii value;
Input : test_list =["geeksforgeeks","is","best"]
Output: test_list =["geeksforgeeks","is","best"]
Explaination: s=s=s<t , sorted by maximum character.
Input: test_list= [" apple","is", fruit"]
output: test_list = ["apple","is","fruit"]
Explaination: p<s<t, hence order retained after sorting by max character.
Code after sorting ;
#above code after sorting the value
def get_max (sub):
return ( ord (max (sub))
test_list = [" geeksforgeeks","is", "best" , "for" ,"cs" ]
print (" the original list is:" + str (test_list))
#performing sorting
test_list.sort (key= get_max)
#printing result
print (" sorted list is" : + str (test_list))
output (after sorting):
Original list is: ["geeksforgeeks","is" ,"best" , " for" ," cs" ]
Sorted list is: [" for" , "geeksforgeeks, "is" , "cs" , "best" ]