+ 1
Use Python for string manipulation and sorting.
You are given a string containing only English letters (uppercase and lowercase) and digits (0-9). Your task is to group the characters in the string as follows: 1. Group all uppercase letters together. 2. Group all lowercase letters together. 3. Group all digits together. Each group should be sorted individually. Finally, concatenate all the groups to form a new sorted string. Example Input: "bA1cD2a3B" Expected Output: "ABDabc123"
3 Réponses
+ 2
Show us your attempts and let us know where you are stuck.
+ 2
Vishal Sharma ,
The task description contains all the steps you have to complete one after the other:
"Each group should be sorted individually. Finally, concatenate all the groups to form a new sorted string."
> we have to get the 3 groups (upper, lower, digit) separated. we can use a loop with a conditional (uppercase, lower...) , or we can use a list comprehension also with a conditional.
(result will be 3 lists of characters)
> sort these 3 lists, and make each of them a `real` string.
> combine the 3 sorted strings
0
maybe this link can give you some hints
https://www.geeksforgeeks.org/JUMP_LINK__&&__python__&&__JUMP_LINK-ways-to-sort-letters-of-string-alphabetically/
but as Lothar said, you have to group the input stings into three categories first. Sorting the string directly will not give you the result you want.