diff --git a/src/__pycache__/string_reversal.cpython-312.pyc b/src/__pycache__/string_reversal.cpython-312.pyc new file mode 100644 index 0000000..776d921 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..654cd85 --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,28 @@ +def reverse_string(input_string): + """ + Reverse a given string manually without using slice notation or reverse(). + + Args: + input_string (str): The string to be reversed. + + Returns: + str: The reversed string. + + Raises: + TypeError: If the input is not a string. + ValueError: If the input string is None. + """ + # Check for None input + if input_string is None: + raise ValueError("Input string cannot be None") + + # Check for non-string input + if not isinstance(input_string, str): + raise TypeError(f"Input must be a string, not {type(input_string).__name__}") + + # Manually reverse the string using a list-based approach + reversed_chars = [] + for char in input_string: + reversed_chars.insert(0, char) + + 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 new file mode 100644 index 0000000..d81d260 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..e6085f9 --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,41 @@ +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 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 string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_string_with_special_chars(): + """Test reversal of string with special characters.""" + assert reverse_string("hello, world!") == "!dlrow ,olleh" + +def test_reverse_string_unicode(): + """Test reversal of unicode strings.""" + assert reverse_string("こんにちは") == "はちにんこ" + +def test_reverse_string_none_input(): + """Test that None input raises a ValueError.""" + with pytest.raises(ValueError, match="Input string cannot be None"): + reverse_string(None) + +def test_reverse_string_non_string_input(): + """Test that non-string input raises a TypeError.""" + 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(["list"]) + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string({"dict": "value"}) \ No newline at end of file