diff --git a/src/__pycache__/string_reversal.cpython-312.pyc b/src/__pycache__/string_reversal.cpython-312.pyc new file mode 100644 index 0000000..ab89b5b Binary files /dev/null and b/src/__pycache__/string_reversal.cpython-312.pyc differ diff --git a/src/string_reversal.py b/src/string_reversal.py new file mode 100644 index 0000000..b5749e9 --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,33 @@ +def reverse_string(input_string): + """ + Reverse a given string using manual character iteration. + + Args: + input_string (str): The string to be reversed. + + Returns: + str: The reversed string. + + Raises: + TypeError: If the input is not a string. + """ + # Validate input is a string + if not isinstance(input_string, str): + raise TypeError("Input must be a string") + + # Handle empty string case + if len(input_string) <= 1: + return input_string + + # Convert string to list for manipulation + chars = list(input_string) + + # Swap characters from start to end + left, right = 0, len(chars) - 1 + while left < right: + chars[left], chars[right] = chars[right], chars[left] + left += 1 + right -= 1 + + # Convert back to string and return + return ''.join(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 new file mode 100644 index 0000000..e0c49d7 Binary files /dev/null and b/tests/__pycache__/test_string_reversal.cpython-312-pytest-8.3.5.pyc differ diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py new file mode 100644 index 0000000..61eb7db --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,35 @@ +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_with_spaces(): + """Test reversing string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_string_special_chars(): + """Test reversing string with special characters.""" + assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" + +def test_reverse_string_single_char(): + """Test reversing a single character string.""" + assert reverse_string("a") == "a" + +def test_reverse_string_mixed_characters(): + """Test reversing 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"): + reverse_string(123) + + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(None) \ No newline at end of file