DEV Community

Discussion on: Daily Challenge #253 - Sort Sentences Pseudo-alphabetically

Collapse
 
vidit1999 profile image
Vidit Sarkar

Here is a Python solution,

from string import punctuation

def sort(s):
    lowerStart = []
    upperStart = []

    for word in ''.join(c for c in s if c not in punctuation).split():
        if(word[0].islower()):
            lowerStart.append(word)
        elif(word[0].isupper()):
            upperStart.append(word)

    return ' '.join(sorted(lowerStart) + sorted(upperStart, reverse=True))

Output,

print(sort("Land of the Old Thirteen! Massachusetts land! land of Vermont and Connecticut!")) # output -> 'and land land of of the Vermont Thirteen Old Massachusetts Land Connecticut'
print(sort("take up the task eternal, and the burden and the lesson")) # output -> 'and and burden eternal lesson take task the the the up'
print(sort("Pioneers, O Pioneers!")) # output -> 'Pioneers Pioneers O'