diff --git a/src/__pycache__/string_reversal.cpython-312.pyc b/src/__pycache__/string_reversal.cpython-312.pyc index 1841456..41b8c3a 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/string_utils.py b/src/string_utils.py index 63a44a0..2bd3d9d 100644 --- a/src/string_utils.py +++ b/src/string_utils.py @@ -1,4 +1,4 @@ -def reverse_string(input_string): +def reverse_string(input_string: str) -> str: """ Reverse the given string using a manual character-by-character approach. @@ -11,14 +11,12 @@ def reverse_string(input_string): Raises: TypeError: If the input is not a string. """ - # Type checking + # Type checking with additional type hints 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]) + # Use efficient list comprehension for reversal + reversed_chars = [input_string[i] for i in range(len(input_string) - 1, -1, -1)] - # Convert list back to string + # Convert list back to string and return return ''.join(reversed_chars) \ 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..bfe2877 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/test_string_utils.py b/tests/test_string_utils.py index 0a66c5b..abde176 100644 --- a/tests/test_string_utils.py +++ b/tests/test_string_utils.py @@ -10,6 +10,10 @@ 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 strings with spaces.""" assert reverse_string("hello world") == "dlrow olleh" @@ -17,6 +21,7 @@ def test_reverse_string_with_spaces(): def test_reverse_string_special_chars(): """Test reversal of strings with special characters.""" assert reverse_string("a!b@c#") == "#c@b!a" + assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" def test_reverse_string_unicode(): """Test reversal of unicode strings."""