From 0372e58f0306e3e19ec48cf1933a2b8f8208f8ed Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 10:17:32 -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 b7bf9ddde8277a949b060880894ff77fd46eac80 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 10:17:46 -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..7ea626ba --- /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(["list"]) \ No newline at end of file From 76c4bd6c73274ff2436c5c8f68c218a73353714b Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 10:18:55 -0300 Subject: [PATCH 3/4] Implement manual string reversal without using slice or reverse() --- src/string_reversal.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/string_reversal.py b/src/string_reversal.py index 84013f1d..71f7ee3b 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, preserving all original characters. Args: s (str): The input string to be reversed. @@ -11,9 +11,13 @@ def reverse_string(s: str) -> str: 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 + # Manual string reversal using list conversion and iteration + reversed_chars = [] + for i in range(len(s) - 1, -1, -1): + reversed_chars.append(s[i]) + + return ''.join(reversed_chars) \ No newline at end of file From 931a338bcdd12a8585d8ebf5121850616ee931a4 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Tue, 22 Apr 2025 10:19:07 -0300 Subject: [PATCH 4/4] Update tests for comprehensive string reversal coverage --- 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 7ea626ba..4f6df057 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 reversal of string with special characters.""" assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" +def test_reverse_string_mixed_characters(): + """Test reversal of 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"):