comments | difficulty | edit_url | tags | |
---|---|---|---|---|
true |
Hard |
|
Table: user_content
+-------------+---------+ | Column Name | Type | +-------------+---------+ | content_id | int | | content_text| varchar | +-------------+---------+ content_id is the unique key for this table. Each row contains a unique ID and the corresponding text content.
Write a solution to transform the text in the content_text
column by applying the following rules:
- Convert the first letter of each word to uppercase and the remaining letters to lowercase
- Special handling for words containing special characters:
- For words connected with a hyphen
-
, both parts should be capitalized (e.g., top-rated → Top-Rated)
- For words connected with a hyphen
- All other formatting and spacing should remain unchanged
Return the result table that includes both the original content_text
and the modified text following the above rules.
The result format is in the following example.
Example:
Input:
user_content table:
+------------+---------------------------------+ | content_id | content_text | +------------+---------------------------------+ | 1 | hello world of SQL | | 2 | the QUICK-brown fox | | 3 | modern-day DATA science | | 4 | web-based FRONT-end development | +------------+---------------------------------+
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"
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"]
]