Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1891ffc

Browse files
authored
Added task 3374
1 parent97be53a commit1891ffc

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
3374\. First Letter Capitalization II
2+
3+
Hard
4+
5+
SQL Schema
6+
7+
Table:`user_content`
8+
9+
+-------------+---------+
10+
| Column Name | Type |
11+
+-------------+---------+
12+
| content_id | int |
13+
| content_text| varchar |
14+
+-------------+---------+
15+
content_id is the unique key for this table. Each row contains a unique ID and the corresponding text content.
16+
17+
Write a solution to transform the text in the`content_text` column by applying the following rules:
18+
19+
* Convert the**first letter** of each word to**uppercase** and the**remaining** letters to**lowercase**
20+
* Special handling for words containing special characters:
21+
* For words connected with a hyphen`-`,**both parts** should be**capitalized** (**e.g.**, top-rated → Top-Rated)
22+
* All other**formatting** and**spacing** should remain**unchanged**
23+
24+
Return_the result table that includes both the original`content_text` and the modified text following the above rules_.
25+
26+
The result format is in the following example.
27+
28+
**Example:**
29+
30+
**Input:**
31+
32+
user\_content table:
33+
34+
+------------+---------------------------------+
35+
| content_id | content_text |
36+
+------------+---------------------------------+
37+
| 1 | hello world of SQL |
38+
| 2 | the QUICK-brown fox |
39+
| 3 | modern-day DATA science |
40+
| 4 | web-based FRONT-end development |
41+
+------------+---------------------------------+
42+
43+
**Output:**
44+
45+
+------------+---------------------------------+---------------------------------+
46+
| content_id | original_text | converted_text |
47+
+------------+---------------------------------+---------------------------------+
48+
| 1 | hello world of SQL | Hello World Of Sql |
49+
| 2 | the QUICK-brown fox | The Quick-Brown Fox |
50+
| 3 | modern-day DATA science | Modern-Day Data Science |
51+
| 4 | web-based FRONT-end development | Web-Based Front-End Development |
52+
+------------+---------------------------------+---------------------------------+
53+
54+
**Explanation:**
55+
56+
* For content\_id = 1:
57+
* Each word's first letter is capitalized: "Hello World Of Sql"
58+
* For content\_id = 2:
59+
* Contains the hyphenated word "QUICK-brown" which becomes "Quick-Brown"
60+
* Other words follow normal capitalization rules
61+
* For content\_id = 3:
62+
* Hyphenated word "modern-day" becomes "Modern-Day"
63+
* "DATA" is converted to "Data"
64+
* For content\_id = 4:
65+
* Contains two hyphenated words: "web-based" → "Web-Based"
66+
* And "FRONT-end" → "Front-End"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# #Hard #Database #2024_12_06_Time_261_ms_(84.21%)_Space_66.3_MB_(17.89%)
2+
3+
importpandasaspd
4+
5+
defcapitalize_content(user_content):
6+
user_content['converted_text']= (user_content.content_text.apply(lambdax:x.title()))
7+
returnuser_content.rename(columns={'content_text':'original_text'})
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
importunittest
2+
importpandasaspd
3+
4+
# Embed the script
5+
defcapitalize_content(user_content):
6+
user_content['converted_text']= (user_content.content_text.apply(lambdax:x.title()))
7+
returnuser_content.rename(columns={'content_text':'original_text'})
8+
9+
# Test suite
10+
classTestCapitalizeContent(unittest.TestCase):
11+
12+
deftest_normal_case(self):
13+
# Input data
14+
data= {
15+
'content_id': [1,2],
16+
'content_text': ['hello world','python programming']
17+
}
18+
df=pd.DataFrame(data)
19+
20+
# Expected output
21+
expected_data= {
22+
'content_id': [1,2],
23+
'original_text': ['hello world','python programming'],
24+
'converted_text': ['Hello World','Python Programming']
25+
}
26+
expected_df=pd.DataFrame(expected_data)
27+
28+
# Test
29+
result=capitalize_content(df)
30+
pd.testing.assert_frame_equal(result,expected_df)
31+
32+
deftest_hyphenated_words(self):
33+
# Input data
34+
data= {
35+
'content_id': [1],
36+
'content_text': ['well-known fact']
37+
}
38+
df=pd.DataFrame(data)
39+
40+
# Expected output
41+
expected_data= {
42+
'content_id': [1],
43+
'original_text': ['well-known fact'],
44+
'converted_text': ['Well-Known Fact']
45+
}
46+
expected_df=pd.DataFrame(expected_data)
47+
48+
# Test
49+
result=capitalize_content(df)
50+
pd.testing.assert_frame_equal(result,expected_df)
51+
52+
deftest_mixed_case(self):
53+
# Input data
54+
data= {
55+
'content_id': [1],
56+
'content_text': ['QUICK-brown FOX']
57+
}
58+
df=pd.DataFrame(data)
59+
60+
# Expected output
61+
expected_data= {
62+
'content_id': [1],
63+
'original_text': ['QUICK-brown FOX'],
64+
'converted_text': ['Quick-Brown Fox']
65+
}
66+
expected_df=pd.DataFrame(expected_data)
67+
68+
# Test
69+
result=capitalize_content(df)
70+
pd.testing.assert_frame_equal(result,expected_df)
71+
72+
deftest_empty_input(self):
73+
# Input data
74+
df=pd.DataFrame(columns=['content_id','content_text'])
75+
76+
# Expected output
77+
expected_df=pd.DataFrame(columns=['content_id','original_text','converted_text'])
78+
79+
# Test
80+
result=capitalize_content(df)
81+
pd.testing.assert_frame_equal(result,expected_df)
82+
83+
deftest_special_characters(self):
84+
# Input data
85+
data= {
86+
'content_id': [1],
87+
'content_text': ['C++ Programming']
88+
}
89+
df=pd.DataFrame(data)
90+
91+
# Expected output
92+
expected_data= {
93+
'content_id': [1],
94+
'original_text': ['C++ Programming'],
95+
'converted_text': ['C++ Programming']
96+
}
97+
expected_df=pd.DataFrame(expected_data)
98+
99+
# Test
100+
result=capitalize_content(df)
101+
pd.testing.assert_frame_equal(result,expected_df)
102+
103+
if__name__=='__main__':
104+
unittest.main()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp