나는 이렇게 학습한다/Algorithm & SQL

Jaden Casing Strings

daco2020 2022. 4. 1. 14:20
반응형

Description:

Jaden Smith, the son of Will Smith, is the star of films such as The Karate Kid (2010) and After Earth (2013). Jaden is also known for some of his philosophy that he delivers via Twitter. When writing on Twitter, he is known for almost always capitalizing every word. For simplicity, you'll have to capitalize each word, check out how contractions are expected to be in the example below.

Your task is to convert strings to how they would be written by Jaden Smith. The strings are actual quotes from Jaden Smith, but they are not capitalized in the same way he originally typed them.

Example:

Not Jaden-Cased: "How can mirrors be real if our eyes aren't real"
Jaden-Cased:     "How Can Mirrors Be Real If Our Eyes Aren't Real"

Link to Jaden's former Twitter account @officialjaden via archive.org

 

 

 

Solution:

1. Change the first letter of each word to uppercase.
2. Returns the replaced string.

 

import re

def to_jaden_case(string):
    return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
           lambda x: x.group(0).capitalize(),
                  string)

At first, I tried to end it easily using title().
However, the title() method had a fatal problem.
Words containing ', for example daco's, are replaced with Daco'S. !!??


When I checked this problem in the official Python documentation, a workaround was already prepared.
Below is a part of the official document.

 

This is a method using regular expressions and lambda.
But I wonder if I even need to do this. :(

 

 

 

 

반응형