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_reversal.cpython-312.pyc b/src/__pycache__/string_reversal.cpython-312.pyc new file mode 100644 index 0000000..1841456 Binary files /dev/null and b/src/__pycache__/string_reversal.cpython-312.pyc differ diff --git a/src/__pycache__/string_utils.cpython-312.pyc b/src/__pycache__/string_utils.cpython-312.pyc new file mode 100644 index 0000000..e7d9724 Binary files /dev/null and b/src/__pycache__/string_utils.cpython-312.pyc differ diff --git a/src/string_reversal.py b/src/string_reversal.py new file mode 100644 index 0000000..4113150 --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,34 @@ +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") + + # Handle empty string case + if not input_string: + return "" + + # 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 from both ends + chars[left], chars[right] = chars[right], chars[left] + left += 1 + right -= 1 + + # Convert list back to string and return + return ''.join(chars) \ No newline at end of file diff --git a/src/string_utils.py b/src/string_utils.py new file mode 100644 index 0000000..679e567 --- /dev/null +++ b/src/string_utils.py @@ -0,0 +1,28 @@ +def reverse_string(input_string: str) -> str: + """ + Reverse the 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. + """ + # Type checking + if not isinstance(input_string, str): + raise TypeError("Input must be a string") + + # Handle empty string case + if not input_string: + return "" + + # Use list to manually reverse the string + reversed_chars = [] + for i in range(len(input_string) - 1, -1, -1): + reversed_chars.append(input_string[i]) + + # Convert list back to string + 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..7d4b01b Binary files /dev/null and b/tests/__pycache__/test_string_reversal.cpython-312-pytest-8.3.5.pyc differ 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..945ab3b 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_reversal.py b/tests/test_string_reversal.py new file mode 100644 index 0000000..61d360c --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,42 @@ +import pytest +from src.string_reversal import reverse_string + +def test_reverse_standard_string(): + """Test reversing a standard string.""" + assert reverse_string("hello") == "olleh" + assert reverse_string("python") == "nohtyp" + +def test_reverse_empty_string(): + """Test reversing an empty string.""" + assert reverse_string("") == "" + +def test_reverse_single_char_string(): + """Test reversing a single character string.""" + assert reverse_string("a") == "a" + +def test_reverse_palindrome(): + """Test reversing a palindrome string.""" + assert reverse_string("racecar") == "racecar" + +def test_reverse_with_spaces(): + """Test reversing a string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_with_special_characters(): + """Test reversing a string with special characters.""" + assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" + +def test_reverse_unicode_string(): + """Test reversing a string with Unicode characters.""" + assert reverse_string("こんにちは") == "はちにんこ" + +def test_invalid_input_type(): + """Test that a TypeError is raised for non-string inputs.""" + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(12345) + + 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 diff --git a/tests/test_string_utils.py b/tests/test_string_utils.py new file mode 100644 index 0000000..0a66c5b --- /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_with_spaces(): + """Test reversal of strings with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_string_special_chars(): + """Test reversal of strings with special characters.""" + assert reverse_string("a!b@c#") == "#c@b!a" + +def test_reverse_string_unicode(): + """Test reversal of unicode strings.""" + assert reverse_string("こんにちは") == "はちにんこ" + +def test_reverse_string_mixed_chars(): + """Test reversal of strings with mixed character types.""" + assert reverse_string("Hello, World! 123") == "321 !dlroW ,olleH" + +def test_reverse_string_invalid_input(): + """Test that a 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