From 35562581bf525b8d08899d08672ecfba5191abb5 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 30 Jan 2025 05:48:23 +0200 Subject: [PATCH] Added task 3374 --- .../readme.md | 66 +++++++++++ .../solution.py | 7 ++ .../solution_test.py | 104 ++++++++++++++++++ 3 files changed, 177 insertions(+) create mode 100644 src/main/kotlin/g3301_3400/s3374_first_letter_capitalization_ii/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3374_first_letter_capitalization_ii/solution.py create mode 100644 src/test/kotlin/g3301_3400/s3374_first_letter_capitalization_ii/solution_test.py diff --git a/src/main/kotlin/g3301_3400/s3374_first_letter_capitalization_ii/readme.md b/src/main/kotlin/g3301_3400/s3374_first_letter_capitalization_ii/readme.md new file mode 100644 index 000000000..27ab1d6a5 --- /dev/null +++ b/src/main/kotlin/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/kotlin/g3301_3400/s3374_first_letter_capitalization_ii/solution.py b/src/main/kotlin/g3301_3400/s3374_first_letter_capitalization_ii/solution.py new file mode 100644 index 000000000..725627c1b --- /dev/null +++ b/src/main/kotlin/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/kotlin/g3301_3400/s3374_first_letter_capitalization_ii/solution_test.py b/src/test/kotlin/g3301_3400/s3374_first_letter_capitalization_ii/solution_test.py new file mode 100644 index 000000000..9eaa4ec50 --- /dev/null +++ b/src/test/kotlin/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()