From 30891c0f4a876cd6b16bb3fc687108ab2d219f5c Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 6 Dec 2024 04:14:49 +0200 Subject: [PATCH 1/2] Added task 3374 --- .../readme.md | 66 +++++++++++ .../solution.py | 7 ++ .../solution_test.py | 104 ++++++++++++++++++ 3 files changed, 177 insertions(+) create mode 100644 src/main/java/g3301_3400/s3374_first_letter_capitalization_ii/readme.md create mode 100644 src/main/java/g3301_3400/s3374_first_letter_capitalization_ii/solution.py create mode 100644 src/test/java/g3301_3400/s3374_first_letter_capitalization_ii/solution_test.py diff --git a/src/main/java/g3301_3400/s3374_first_letter_capitalization_ii/readme.md b/src/main/java/g3301_3400/s3374_first_letter_capitalization_ii/readme.md new file mode 100644 index 000000000..99b00d779 --- /dev/null +++ b/src/main/java/g3301_3400/s3374_first_letter_capitalization_ii/readme.md @@ -0,0 +1,66 @@ +3374\. First Letter Capitalization II + +Hard + +SQL Schema + +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) +* 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" \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3374_first_letter_capitalization_ii/solution.py b/src/main/java/g3301_3400/s3374_first_letter_capitalization_ii/solution.py new file mode 100644 index 000000000..725627c1b --- /dev/null +++ b/src/main/java/g3301_3400/s3374_first_letter_capitalization_ii/solution.py @@ -0,0 +1,7 @@ +# #Hard #Database #2024_12_06_Time_261_ms_(84.21%)_Space_66.3_MB_(17.89%) + +import pandas as pd + +def capitalize_content(user_content): + user_content['converted_text'] = (user_content.content_text.apply(lambda x: x.title())) + return user_content.rename(columns={'content_text': 'original_text'}) diff --git a/src/test/java/g3301_3400/s3374_first_letter_capitalization_ii/solution_test.py b/src/test/java/g3301_3400/s3374_first_letter_capitalization_ii/solution_test.py new file mode 100644 index 000000000..9eaa4ec50 --- /dev/null +++ b/src/test/java/g3301_3400/s3374_first_letter_capitalization_ii/solution_test.py @@ -0,0 +1,104 @@ +import unittest +import pandas as pd + +# Embed the script +def capitalize_content(user_content): + user_content['converted_text'] = (user_content.content_text.apply(lambda x: x.title())) + return user_content.rename(columns={'content_text': 'original_text'}) + +# Test suite +class TestCapitalizeContent(unittest.TestCase): + + def test_normal_case(self): + # Input data + data = { + 'content_id': [1, 2], + 'content_text': ['hello world', 'python programming'] + } + df = pd.DataFrame(data) + + # Expected output + expected_data = { + 'content_id': [1, 2], + 'original_text': ['hello world', 'python programming'], + 'converted_text': ['Hello World', 'Python Programming'] + } + expected_df = pd.DataFrame(expected_data) + + # Test + result = capitalize_content(df) + pd.testing.assert_frame_equal(result, expected_df) + + def test_hyphenated_words(self): + # Input data + data = { + 'content_id': [1], + 'content_text': ['well-known fact'] + } + df = pd.DataFrame(data) + + # Expected output + expected_data = { + 'content_id': [1], + 'original_text': ['well-known fact'], + 'converted_text': ['Well-Known Fact'] + } + expected_df = pd.DataFrame(expected_data) + + # Test + result = capitalize_content(df) + pd.testing.assert_frame_equal(result, expected_df) + + def test_mixed_case(self): + # Input data + data = { + 'content_id': [1], + 'content_text': ['QUICK-brown FOX'] + } + df = pd.DataFrame(data) + + # Expected output + expected_data = { + 'content_id': [1], + 'original_text': ['QUICK-brown FOX'], + 'converted_text': ['Quick-Brown Fox'] + } + expected_df = pd.DataFrame(expected_data) + + # Test + result = capitalize_content(df) + pd.testing.assert_frame_equal(result, expected_df) + + def test_empty_input(self): + # Input data + df = pd.DataFrame(columns=['content_id', 'content_text']) + + # Expected output + expected_df = pd.DataFrame(columns=['content_id', 'original_text', 'converted_text']) + + # Test + result = capitalize_content(df) + pd.testing.assert_frame_equal(result, expected_df) + + def test_special_characters(self): + # Input data + data = { + 'content_id': [1], + 'content_text': ['C++ Programming'] + } + df = pd.DataFrame(data) + + # Expected output + expected_data = { + 'content_id': [1], + 'original_text': ['C++ Programming'], + 'converted_text': ['C++ Programming'] + } + expected_df = pd.DataFrame(expected_data) + + # Test + result = capitalize_content(df) + pd.testing.assert_frame_equal(result, expected_df) + +if __name__ == '__main__': + unittest.main() From f62e6b95ecf9865eac600a40241bf40228ced1a3 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 6 Dec 2024 04:17:33 +0200 Subject: [PATCH 2/2] Updated readme --- .../readme.md | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/main/java/g3301_3400/s3374_first_letter_capitalization_ii/readme.md b/src/main/java/g3301_3400/s3374_first_letter_capitalization_ii/readme.md index 99b00d779..27ab1d6a5 100644 --- a/src/main/java/g3301_3400/s3374_first_letter_capitalization_ii/readme.md +++ b/src/main/java/g3301_3400/s3374_first_letter_capitalization_ii/readme.md @@ -6,12 +6,12 @@ SQL Schema Table: `user_content` -+-------------+---------+ -| Column Name | Type | -+-------------+---------+ -| content_id | int | -| content_text| varchar | -+-------------+---------+ + +-------------+---------+ + | 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: @@ -31,25 +31,25 @@ The result format is in the following example. 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 | -+------------+---------------------------------+ + +------------+---------------------------------+ + | 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 | -+------------+---------------------------------+---------------------------------+ + +------------+---------------------------------+---------------------------------+ + | 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:**