Wednesday, March 16, 2022

Sort the inner content in descending order Python Codewars

Task:

You have to sort the inner content of every word of a string in descending order.

The inner content is the content of a word without first and the last char.

Some examples:

"sort the inner content in descending order"  -->  "srot the inner ctonnet in dsnnieedcg oredr"
"wait for me"        -->  "wiat for me"
"this kata is easy"  -->  "tihs ktaa is esay"

Words are made up of lowercase letters and are separated by a single space. There will be no leading/trailing spaces.

Solution:


def sort_the_inner_content(str):
    words = str.split()
    output = []
    
    for word in words:
        if len(word) > 2:
            output.append(word[0] + ''.join(sorted(word[1:-1], reverse=True)) + word[-1])            
        else: output.append(word)
        
    return ' '.join(output)
print(sort_the_inner_content('sort the inner content i descending order'))

Training on Srot the inner ctonnet in dsnnieedcg oredr | Codewars



Related Posts


No comments :

Post a Comment