From 749c771a33c259dd866c57421edc793f4cfe34bc Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 11:23:13 -0300 Subject: [PATCH 1/4] Add string reversal function implementation --- src/string_reversal.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/string_reversal.py diff --git a/src/string_reversal.py b/src/string_reversal.py new file mode 100644 index 00000000..6c5177aa --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,19 @@ +def reverse_string(s: str) -> str: + """ + Reverse the given string. + + Args: + s (str): The input string to be reversed. + + Returns: + str: The reversed string. + + Raises: + TypeError: If the input is not a string. + """ + # Check if input is a string + if not isinstance(s, str): + raise TypeError("Input must be a string") + + # Return the reversed string + return s[::-1] \ No newline at end of file From 16f6b7a6ff88da39582c69de72d44bad3917c4fc Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 11:23:24 -0300 Subject: [PATCH 2/4] Add comprehensive tests for string reversal function --- tests/test_string_reversal.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/test_string_reversal.py diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py new file mode 100644 index 00000000..b215bd58 --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,34 @@ +import pytest +from src.string_reversal import reverse_string + +def test_reverse_string_basic(): + """Test basic string reversal.""" + assert reverse_string("hello") == "olleh" + assert reverse_string("world") == "dlrow" + +def test_reverse_string_empty(): + """Test reversing an empty string.""" + assert reverse_string("") == "" + +def test_reverse_string_single_char(): + """Test reversing a single character string.""" + assert reverse_string("a") == "a" + +def test_reverse_string_with_spaces(): + """Test reversing a string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_string_with_special_chars(): + """Test reversing a string with special characters.""" + assert reverse_string("a1b2c3") == "3c2b1a" + +def test_reverse_string_invalid_input(): + """Test that TypeError is raised for non-string inputs.""" + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(123) + + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(None) + + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(["list"]) \ No newline at end of file From 96042d1c47dfde5620e4b5510d36b38e48a4c800 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 11:24:33 -0300 Subject: [PATCH 3/4] Implement manual string reversal with two-pointer approach --- src/string_reversal.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/string_reversal.py b/src/string_reversal.py index 6c5177aa..bf57d9bc 100644 --- a/src/string_reversal.py +++ b/src/string_reversal.py @@ -1,6 +1,6 @@ def reverse_string(s: str) -> str: """ - Reverse the given string. + Reverse the given string using a manual character-by-character approach. Args: s (str): The input string to be reversed. @@ -11,9 +11,27 @@ def reverse_string(s: str) -> str: Raises: TypeError: If the input is not a string. """ - # Check if input is a string + # Type checking if not isinstance(s, str): raise TypeError("Input must be a string") - # Return the reversed string - return s[::-1] \ No newline at end of file + # Handle empty string case + if len(s) <= 1: + return s + + # Manual string reversal + # Convert string to list of characters for easy manipulation + chars = list(s) + + # Two-pointer approach to swap characters + left, right = 0, len(chars) - 1 + while left < right: + # Swap characters + chars[left], chars[right] = chars[right], chars[left] + + # Move pointers + left += 1 + right -= 1 + + # Convert list back to string and return + return ''.join(chars) \ No newline at end of file From 9b535cd12b26df81940a15b3e479d2fe38d5395e Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 11:24:42 -0300 Subject: [PATCH 4/4] Update tests for comprehensive string reversal testing --- tests/test_string_reversal.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py index b215bd58..e6e96d9d 100644 --- a/tests/test_string_reversal.py +++ b/tests/test_string_reversal.py @@ -20,7 +20,11 @@ def test_reverse_string_with_spaces(): def test_reverse_string_with_special_chars(): """Test reversing a string with special characters.""" - assert reverse_string("a1b2c3") == "3c2b1a" + assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" + +def test_reverse_string_with_mixed_chars(): + """Test reversing a string with mixed characters.""" + assert reverse_string("Hello, World! 123") == "321 !dlroW ,olleH" def test_reverse_string_invalid_input(): """Test that TypeError is raised for non-string inputs."""