From 3943a606ab19b7c552c13b92cd0dad56c16328cd Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 6 May 2025 12:38:55 -0300 Subject: [PATCH 1/7] Start draft PR From e60cfab8b1db3900ae35859fca56457bd74b7094 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 6 May 2025 12:39:09 -0300 Subject: [PATCH 2/7] 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 8953a87688d89676ef14a71450fcbcf16b80d2dc Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 6 May 2025 12:39:19 -0300 Subject: [PATCH 3/7] Add comprehensive tests for string reversal function --- tests/test_string_reversal.py | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 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..eec4f28d --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,38 @@ +import pytest +from src.string_reversal import reverse_string + +def test_reverse_standard_string(): + """Test reversing a standard ASCII string.""" + assert reverse_string("hello") == "olleh" + assert reverse_string("python") == "nohtyp" + +def test_reverse_empty_string(): + """Test reversing an empty string.""" + assert reverse_string("") == "" + +def test_reverse_palindrome(): + """Test reversing a palindrome.""" + assert reverse_string("racecar") == "racecar" + +def test_reverse_with_spaces(): + """Test reversing a string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_with_special_characters(): + """Test reversing a string with special characters.""" + assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" + +def test_reverse_unicode_string(): + """Test reversing a string with unicode characters.""" + assert reverse_string("こんにちは") == "はちにんこ" + +def test_invalid_input_type(): + """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 aeabe7950956cc7e159b535954202aea4c7e0948 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 6 May 2025 12:40:00 -0300 Subject: [PATCH 4/7] Implement string reversal with manual two-pointer approach --- src/string_reversal.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/string_reversal.py b/src/string_reversal.py index 84013f1d..59737f62 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 manually without using slice notation or reverse(). Args: s (str): The input string to be reversed. @@ -11,9 +11,20 @@ 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 manual reversal + chars = list(s) + + # Manually reverse the list of characters + left, right = 0, len(chars) - 1 + while left < right: + # Swap characters from both ends + chars[left], chars[right] = chars[right], chars[left] + left += 1 + right -= 1 + + # Convert back to string and return + return ''.join(chars) \ No newline at end of file From fc121ddbaff43bf4e28e7d0b15d1e6a284fadb35 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 6 May 2025 12:40:09 -0300 Subject: [PATCH 5/7] Update test cases for string reversal function --- tests/test_string_reversal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py index eec4f28d..98995a2c 100644 --- a/tests/test_string_reversal.py +++ b/tests/test_string_reversal.py @@ -35,4 +35,4 @@ def test_invalid_input_type(): reverse_string(None) with pytest.raises(TypeError, match="Input must be a string"): - reverse_string(["hello"]) \ No newline at end of file + reverse_string(["hello"])) \ No newline at end of file From 0a210f369f5428f9e0e5924f7b6dfaa25349644b Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 6 May 2025 12:40:21 -0300 Subject: [PATCH 6/7] Fix syntax error in test file --- tests/test_string_reversal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py index 98995a2c..eec4f28d 100644 --- a/tests/test_string_reversal.py +++ b/tests/test_string_reversal.py @@ -35,4 +35,4 @@ def test_invalid_input_type(): reverse_string(None) with pytest.raises(TypeError, match="Input must be a string"): - reverse_string(["hello"])) \ No newline at end of file + reverse_string(["hello"]) \ No newline at end of file From 76b90eb7f3ccacc35a204dcffa787ec9dc851e3e Mon Sep 17 00:00:00 2001 From: momstrosity Date: Tue, 6 May 2025 12:41:41 -0300 Subject: [PATCH 7/7] Start draft PR