Welcome to Subscribe On Youtube
3374. First Letter Capitalization II
Description
Table: user_content
+-++ \| content_id \| int \| \| content_text\| varchar \| +-++ \| content_id \| content_text \| +++
Output:
+++ \| content_id \| original_text \| converted_text \| +++ \| 1 \| hello world of SQL \| Hello World Of Sql \| \| 2 \| the QUICK-brown fox \| The Quick-Brown Fox \| \| 3 \| modern-day DATA science \| Modern-Day Data Science \| \| 4 \| web-based FRONT-end development \| Web-Based Front-End Development \| +++
Explanation:
- For content_id = 1:
- Each word's first letter is capitalized: "Hello World Of Sql"
- For content_id = 2:
- Contains the hyphenated word "QUICK-brown" which becomes "Quick-Brown"
- Other words follow normal capitalization rules
- For content_id = 3:
- Hyphenated word "modern-day" becomes "Modern-Day"
- "DATA" is converted to "Data"
- For content_id = 4:
- Contains two hyphenated words: "web-based" → "Web-Based"
- And "FRONT-end" → "Front-End"
</div>
Solutions
Solution 1
-
import pandas as pd def capitalize_content(user_content: pd.DataFrame) -> pd.DataFrame: def convert_text(text: str) -> str: return " ".join( ( "-".join([part.capitalize() for part in word.split("-")]) if "-" in word else word.capitalize() ) for word in text.split(" ") ) user_content["converted_text"] = user_content["content_text"].apply(convert_text) return user_content.rename(columns={"content_text": "original_text"})[ ["content_id", "original_text", "converted_text"] ]