DEV Community

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

Collapse
 
rrampage profile image
Raunak Ramakrishnan • Edited

Python

import re

# Pat to check if first letter is capital
pat = re.compile("[A-Z].*")

def pseudo_alphabet(s):
    # Replace all non alphabets and spaces from sentence
    ss = re.sub("[^a-zA-Z0-9 ]+", "", s)
    # Split by space
    l = ss.split()
    # Separate lists for upper and lower case words
    uw, lw = [], []
    for w in l:
        # If word matches first letter upper-case, add to "uw" else to "lw"
        if pat.match(w):
            uw.append(w) 
        else: 
             lw.append(w)
    return ' '.join(sorted(lw)) + ' ' + ' '.join(sorted(uw, reverse=True))

print(pseudo_alphabet(s))