diff --git a/src/__pycache__/string_utils.cpython-312.pyc b/src/__pycache__/string_utils.cpython-312.pyc index aca1ee1..ec32751 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..a16ab6e 100644 --- a/src/string_reversal.py +++ b/src/string_reversal.py @@ -1,4 +1,4 @@ -def reverse_string(input_string): +def reverse_string(input_string: str) -> str: """ Reverse the given input string manually, without using slice notation or reverse(). @@ -15,10 +15,14 @@ def reverse_string(input_string): if not isinstance(input_string, str): raise TypeError("Input must be a string") - # Convert string to list of characters + # Handle empty string case + if not input_string: + return "" + + # Convert string to list of characters for manipulation chars = list(input_string) - # Manually reverse the list of characters + # Reverse the list 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..24e5584 100644 --- a/src/string_utils.py +++ b/src/string_utils.py @@ -11,14 +11,12 @@ def reverse_string(input_string): Raises: TypeError: If the input is not a string. """ - # Type checking + # Enhanced type checking with more explicit validation 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]) + # Efficient string reversal using list comprehension + reversed_chars = [input_string[i] for i in range(len(input_string) - 1, -1, -1)] # Convert list back to string return ''.join(reversed_chars) \ No newline at end of file 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..ca220a9 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..9db46af 100644 --- a/tests/test_string_reversal.py +++ b/tests/test_string_reversal.py @@ -9,29 +9,32 @@ def test_reverse_empty_string(): """Test reversing an empty string.""" assert reverse_string("") == "" +def test_reverse_single_char_string(): + """Test reversing a single character string.""" + assert reverse_string("a") == "a" + def test_reverse_palindrome(): - """Test reversing a palindrome string.""" + """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(): +def test_reverse_with_special_chars(): """Test reversing a string with special characters.""" + assert reverse_string("hello!@#") == "#@!olleh" assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" def test_reverse_unicode_string(): - """Test reversing a string with Unicode characters.""" + """Test reversing a Unicode string.""" assert reverse_string("こんにちは") == "はちにんこ" def test_invalid_input_type(): - """Test that a TypeError is raised for non-string inputs.""" + """Test raising TypeError for non-string input.""" with pytest.raises(TypeError, match="Input must be a string"): - reverse_string(12345) - + 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