diff --git a/src/__pycache__/string_reversal.cpython-312.pyc b/src/__pycache__/string_reversal.cpython-312.pyc new file mode 100644 index 0000000..d5c483b 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..796db82 --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,31 @@ +def reverse_string(input_str): + """ + Reverse a given string manually without using built-in reversal methods. + + Args: + input_str (str): The string to be reversed. + + Returns: + str: The reversed string. + + Raises: + TypeError: If input is not a string. + """ + # Check if input is a string + if not isinstance(input_str, str): + raise TypeError("Input must be a string") + + # Convert string to list of characters for manipulation + chars = list(input_str) + + # Manual reversal using two-pointer technique + left, right = 0, len(chars) - 1 + while left < right: + # Swap characters + chars[left], chars[right] = chars[right], chars[left] + # Move pointers + left += 1 + right -= 1 + + # Convert back to string + 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..903f632 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..da07575 --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,38 @@ +import pytest +from src.string_reversal import reverse_string + +def test_reverse_simple_string(): + """Test reversing a simple string.""" + assert reverse_string("hello") == "olleh" + +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_string_with_spaces(): + """Test reversing a string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_string_with_special_characters(): + """Test reversing a string with special characters.""" + assert reverse_string("hello, world!") == "!dlrow ,olleh" + +def test_reverse_unicode_string(): + """Test reversing a string with Unicode characters.""" + assert reverse_string("café") == "éfac" + +def test_invalid_input_type(): + """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) + +def test_numeric_string(): + """Test reversing a string of numbers.""" + assert reverse_string("12345") == "54321" \ No newline at end of file