diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..cd328bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +__pycache__/ +*.pyc +*.pyo +*.pyd +.pytest_cache/ +.venv/ +venv/ +dist/ +build/ +*.egg-info/ +.env +.DS_Store \ No newline at end of file diff --git a/src/string_utils.py b/src/string_utils.py new file mode 100644 index 00000000..112fd1ae --- /dev/null +++ b/src/string_utils.py @@ -0,0 +1,32 @@ +def reverse_string(input_string: str) -> str: + """ + Reverse the given input 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. + """ + # Check if input is a string + if not isinstance(input_string, str): + raise TypeError("Input must be a string") + + # Convert string to list of characters + chars = list(input_string) + + # Manually reverse the list of characters + left, right = 0, len(chars) - 1 + while left < right: + # Swap characters + chars[left], chars[right] = chars[right], chars[left] + + # Move towards the center + left += 1 + right -= 1 + + # Convert back to string and return + return ''.join(chars) \ No newline at end of file diff --git a/tests/test_string_utils.py b/tests/test_string_utils.py new file mode 100644 index 00000000..602313f4 --- /dev/null +++ b/tests/test_string_utils.py @@ -0,0 +1,38 @@ +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_single_char(): + """Test reversal of a single character string.""" + 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("a1b2c3!@#") == "#@!3c2b1a" + +def test_reverse_string_unicode(): + """Test reversal of string with unicode characters.""" + assert reverse_string("こんにちは") == "はちにんこ" + +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) + + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(["hello"]) \ No newline at end of file