diff --git a/src/__pycache__/string_reversal.cpython-312.pyc b/src/__pycache__/string_reversal.cpython-312.pyc index 1841456..3102376 100644 Binary files a/src/__pycache__/string_reversal.cpython-312.pyc and b/src/__pycache__/string_reversal.cpython-312.pyc differ diff --git a/src/__pycache__/string_utils.cpython-312.pyc b/src/__pycache__/string_utils.cpython-312.pyc index aca1ee1..336341f 100644 Binary files a/src/__pycache__/string_utils.cpython-312.pyc and b/src/__pycache__/string_utils.cpython-312.pyc differ diff --git a/src/string_reversal.py b/src/string_reversal.py index 952ccb9..61a1eb6 100644 --- a/src/string_reversal.py +++ b/src/string_reversal.py @@ -1,6 +1,11 @@ -def reverse_string(input_string): +def reverse_string(input_string: str) -> str: """ - Reverse the given input string manually, without using slice notation or reverse(). + Reverse the given input string manually, without using slice notation or built-in reverse methods. + + This implementation provides a robust, type-hinted string reversal method that: + - Performs type checking + - Uses an efficient two-pointer swap technique + - Works with various string types, including Unicode Args: input_string (str): The string to be reversed. @@ -15,10 +20,10 @@ def reverse_string(input_string): if not isinstance(input_string, str): raise TypeError("Input must be a string") - # Convert string to list of characters + # Convert string to list of characters for efficient manipulation chars = list(input_string) - # Manually reverse the list of characters + # Manually reverse the list of characters using two-pointer technique left, right = 0, len(chars) - 1 while left < right: # Swap characters from both ends diff --git a/src/string_utils.py b/src/string_utils.py index 63a44a0..2d8a49f 100644 --- a/src/string_utils.py +++ b/src/string_utils.py @@ -1,24 +1,7 @@ -def reverse_string(input_string): - """ - Reverse the given string using a manual character-by-character approach. +# This file has been deprecated in favor of string_reversal.py +# The string reversal functionality is now maintained in string_reversal.py - Args: - input_string (str): The string to be reversed. +from .string_reversal import reverse_string - Returns: - str: The reversed string. - - Raises: - TypeError: If the input is not a string. - """ - # Type checking - if not isinstance(input_string, str): - raise TypeError("Input must be a string") - - # Use list to manually reverse the string - reversed_chars = [] - for i in range(len(input_string) - 1, -1, -1): - reversed_chars.append(input_string[i]) - - # Convert list back to string - return ''.join(reversed_chars) \ No newline at end of file +# Keeping this for backwards compatibility +__all__ = ['reverse_string'] \ No newline at end of file diff --git a/tests/__pycache__/test_string_reversal.cpython-312-pytest-8.3.5.pyc b/tests/__pycache__/test_string_reversal.cpython-312-pytest-8.3.5.pyc index 7d4b01b..d6a2653 100644 Binary files a/tests/__pycache__/test_string_reversal.cpython-312-pytest-8.3.5.pyc and b/tests/__pycache__/test_string_reversal.cpython-312-pytest-8.3.5.pyc differ diff --git a/tests/__pycache__/test_string_utils.cpython-312-pytest-8.3.5.pyc b/tests/__pycache__/test_string_utils.cpython-312-pytest-8.3.5.pyc index 3246c7f..a59234f 100644 Binary files a/tests/__pycache__/test_string_utils.cpython-312-pytest-8.3.5.pyc and b/tests/__pycache__/test_string_utils.cpython-312-pytest-8.3.5.pyc differ diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py index 5398e44..d688f24 100644 --- a/tests/test_string_reversal.py +++ b/tests/test_string_reversal.py @@ -9,6 +9,10 @@ def test_reverse_empty_string(): """Test reversing an empty string.""" assert reverse_string("") == "" +def test_reverse_single_character(): + """Test reversing a single character string.""" + assert reverse_string("a") == "a" + def test_reverse_palindrome(): """Test reversing a palindrome string.""" assert reverse_string("racecar") == "racecar"