From 476ca6eac4511ae62c6a303885f9b0f68260e478 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 11:18:11 -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..84013f1d --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,19 @@ +def reverse_string(s: str) -> str: + """ + Reverse a 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 bcf69ce425c6822a25c6def06d9680784016cf03 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 11:18:23 -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..60273622 --- /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("python") == "nohtyp" + +def test_reverse_string_empty(): + """Test reversing an empty string.""" + assert reverse_string("") == "" + +def test_reverse_string_single_char(): + """Test reversing a single character.""" + 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(["hello"]) \ No newline at end of file From 63f45efe334611d8cddc972f10ccf7d309ce2ee1 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 11:19:16 -0300 Subject: [PATCH 3/4] Reimplement string reversal function with two-pointer technique --- src/string_reversal.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/string_reversal.py b/src/string_reversal.py index 84013f1d..dd84f33f 100644 --- a/src/string_reversal.py +++ b/src/string_reversal.py @@ -1,6 +1,6 @@ def reverse_string(s: str) -> str: """ - Reverse a given string. + Reverse a given string character by character. Args: s (str): The input string to be reversed. @@ -11,9 +11,21 @@ def reverse_string(s: str) -> str: Raises: TypeError: If the input is not a string. """ - # Check if input is a string + # Validate 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 + # Convert string to list of characters for manipulation + chars = list(s) + + # Reverse the list in-place using two-pointer technique + 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 back to string and return + return ''.join(chars) \ No newline at end of file From 5f9c3444a6531a6a8c2eec67b436e583a2d2c494 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 11:19:26 -0300 Subject: [PATCH 4/4] Update tests for string reversal function --- tests/test_string_reversal.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py index 60273622..e4166537 100644 --- a/tests/test_string_reversal.py +++ b/tests/test_string_reversal.py @@ -22,6 +22,10 @@ def test_reverse_string_with_special_chars(): """Test reversing a string with special characters.""" assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" +def test_reverse_string_mixed_characters(): + """Test reversing a string with mixed character types.""" + assert reverse_string("Hello, World! 123") == "321 !dlroW ,olleH" + 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"):