diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..55b033e --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pytest \ No newline at end of file diff --git a/src/__pycache__/string_utils.cpython-312.pyc b/src/__pycache__/string_utils.cpython-312.pyc new file mode 100644 index 0000000..df8cf61 Binary files /dev/null and b/src/__pycache__/string_utils.cpython-312.pyc differ diff --git a/src/string_utils.py b/src/string_utils.py new file mode 100644 index 0000000..2f9d6bd --- /dev/null +++ b/src/string_utils.py @@ -0,0 +1,26 @@ +def reverse_string(input_string): + """ + Reverse a given string using a manual character-by-character approach. + + 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") + + # Use a list to build the reversed string + reversed_chars = [] + + # Iterate through the string from end to beginning + for i in range(len(input_string) - 1, -1, -1): + reversed_chars.append(input_string[i]) + + # Convert list of characters 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 new file mode 100644 index 0000000..64d6bb3 Binary files /dev/null and b/tests/__pycache__/test_string_utils.cpython-312-pytest-8.3.5.pyc differ diff --git a/tests/test_string_utils.py b/tests/test_string_utils.py new file mode 100644 index 0000000..52c4538 --- /dev/null +++ b/tests/test_string_utils.py @@ -0,0 +1,32 @@ +import pytest +from src.string_utils 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_special_chars(): + """Test reversal with special characters and spaces.""" + assert reverse_string("Hello, World!") == "!dlroW ,olleH" + assert reverse_string(" trim ") == " mirt " + +def test_reverse_string_unicode(): + """Test reversal with Unicode characters.""" + assert reverse_string("こんにちは") == "はちにんこ" + assert reverse_string("🌈🦄") == "🦄🌈" + +def test_reverse_string_invalid_input(): + """Test error handling 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) + + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(["list"]) \ No newline at end of file