From 55132f9f4c932999eebb74916222f83a4509f48e Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 11:58:55 -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..0e1a21b3 --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,19 @@ +def reverse_string(s: str) -> str: + """ + Reverses the input 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 9b559042a938a0ab44cccb82ac36a240e011caa2 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 11:59:05 -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..f189bea6 --- /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 reversal of an empty string.""" + assert reverse_string("") == "" + +def test_reverse_string_single_char(): + """Test reversal of a single character.""" + assert reverse_string("a") == "a" + +def test_reverse_string_with_spaces(): + """Test reversal of string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_string_with_special_chars(): + """Test reversal of 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 92f700efc1f410986d52935d7d8e2a08df89179a Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 12:00:02 -0300 Subject: [PATCH 3/4] Implement string reversal without slice notation or reverse() --- src/string_reversal.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/string_reversal.py b/src/string_reversal.py index 0e1a21b3..5db4c238 100644 --- a/src/string_reversal.py +++ b/src/string_reversal.py @@ -1,6 +1,6 @@ def reverse_string(s: str) -> str: """ - Reverses the input string. + Reverses the input string using a manual character-by-character approach. Args: s (str): The input string to be reversed. @@ -15,5 +15,15 @@ def reverse_string(s: str) -> str: 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 and reverse manually + chars = list(s) + 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 4032a8d3406dbb0a9e551303922351cdf41ffed1 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 12:00:10 -0300 Subject: [PATCH 4/4] Update tests for string reversal implementation