From de7f5cbc2cd31abf612f12bd9ec6a96a62d7ddda Mon Sep 17 00:00:00 2001 From: laura-abro Date: Fri, 11 Apr 2025 12:49:13 +0000 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 18db3da003d7c8d859fbfdd91da7349a441f48fc Mon Sep 17 00:00:00 2001 From: laura-abro Date: Fri, 11 Apr 2025 12:49:59 +0000 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..91c92896 --- /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 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_punctuation(): + """Test reversing a string with punctuation.""" + assert reverse_string("hello, world!") == "!dlrow ,olleh" + +def test_reverse_string_invalid_input(): + """Test that a 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 3c52129d5cfe4d34e17f634b841a6d8c24f76599 Mon Sep 17 00:00:00 2001 From: laura-abro Date: Fri, 11 Apr 2025 12:53:11 +0000 Subject: [PATCH 3/4] Implement string reversal without built-in reversal methods --- src/string_reversal.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/string_reversal.py b/src/string_reversal.py index 6c5177aa..c6d47662 100644 --- a/src/string_reversal.py +++ b/src/string_reversal.py @@ -1,19 +1,32 @@ def reverse_string(s: str) -> str: """ - Reverse the given string. - + Reverse the given string using manual character iteration. + 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 + # Validate input 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) + + # Perform manual reversal 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 fab5bdf01830b74f1ef375bc2b81577c2812ce8d Mon Sep 17 00:00:00 2001 From: laura-abro Date: Fri, 11 Apr 2025 12:53:34 +0000 Subject: [PATCH 4/4] Update tests for string reversal implementation --- tests/test_string_reversal.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py index 91c92896..bd2081e4 100644 --- a/tests/test_string_reversal.py +++ b/tests/test_string_reversal.py @@ -19,8 +19,13 @@ def test_reverse_string_with_spaces(): assert reverse_string("hello world") == "dlrow olleh" def test_reverse_string_with_punctuation(): - """Test reversing a string with punctuation.""" + """Test reversing a string with punctuation and mixed characters.""" assert reverse_string("hello, world!") == "!dlrow ,olleh" + assert reverse_string("a1b2c3") == "3c2b1a" + +def test_reverse_string_with_mixed_characters(): + """Test reversing a string with mixed character types.""" + assert reverse_string("abc123!@#") == "#@!321cba" def test_reverse_string_invalid_input(): """Test that a TypeError is raised for non-string inputs.""" @@ -31,4 +36,4 @@ def test_reverse_string_invalid_input(): reverse_string(None) with pytest.raises(TypeError, match="Input must be a string"): - reverse_string(["list"]) \ No newline at end of file + reverse_string(["list"])