From fe86e2d6de0a4d297b5e1f9d0bb6bb310f24d159 Mon Sep 17 00:00:00 2001 From: testtesting123987654 Date: Mon, 26 May 2025 22:22:59 -0300 Subject: [PATCH 1/7] Start draft PR From 0aa4049c02eff53187f67d81e7bcf809e8d504e3 Mon Sep 17 00:00:00 2001 From: testtesting123987654 Date: Mon, 26 May 2025 22:23:09 -0300 Subject: [PATCH 2/7] Add .gitignore file to exclude unnecessary files from version control --- .gitignore | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .gitignore 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 From 42aa5a527b53e945f5dea7ff06047ce01af0b946 Mon Sep 17 00:00:00 2001 From: testtesting123987654 Date: Mon, 26 May 2025 22:23:15 -0300 Subject: [PATCH 3/7] Implement string reversal function with type checking and docstring --- src/string_utils.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/string_utils.py diff --git a/src/string_utils.py b/src/string_utils.py new file mode 100644 index 00000000..1dfb1bff --- /dev/null +++ b/src/string_utils.py @@ -0,0 +1,19 @@ +def reverse_string(input_string: str) -> str: + """ + Reverse the given input string. + + 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") + + # Return the reversed string + return input_string[::-1] \ No newline at end of file From bf3e89dbcb537b44cb70664db94b3191ac88acab Mon Sep 17 00:00:00 2001 From: testtesting123987654 Date: Mon, 26 May 2025 22:23:24 -0300 Subject: [PATCH 4/7] Add comprehensive tests for string reversal function --- tests/test_string_utils.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/test_string_utils.py 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 From 406d3c3e03adc0ff2ce91e540a70635a23f21d02 Mon Sep 17 00:00:00 2001 From: testtesting123987654 Date: Mon, 26 May 2025 22:24:03 -0300 Subject: [PATCH 5/7] Implement string reversal using manual two-pointer swap method --- src/string_utils.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/string_utils.py b/src/string_utils.py index 1dfb1bff..112fd1ae 100644 --- a/src/string_utils.py +++ b/src/string_utils.py @@ -1,6 +1,6 @@ def reverse_string(input_string: str) -> str: """ - Reverse the given input string. + Reverse the given input string manually, without using slice notation or reverse(). Args: input_string (str): The string to be reversed. @@ -15,5 +15,18 @@ def reverse_string(input_string: str) -> str: if not isinstance(input_string, str): raise TypeError("Input must be a string") - # Return the reversed string - return input_string[::-1] \ No newline at end of file + # 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 From bd868c634160b8e58a5b58cd2805cf8cad3d0b0a Mon Sep 17 00:00:00 2001 From: testtesting123987654 Date: Mon, 26 May 2025 22:24:11 -0300 Subject: [PATCH 6/7] Update tests for string reversal function --- tests/test_string_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_string_utils.py b/tests/test_string_utils.py index 602313f4..5ea0062d 100644 --- a/tests/test_string_utils.py +++ b/tests/test_string_utils.py @@ -35,4 +35,4 @@ def test_reverse_string_invalid_input(): reverse_string(None) with pytest.raises(TypeError, match="Input must be a string"): - reverse_string(["hello"]) \ No newline at end of file + reverse_string(["hello"])) \ No newline at end of file From cbd1b1e5e7c611b32c86de743de8f23c2c224c7c Mon Sep 17 00:00:00 2001 From: testtesting123987654 Date: Mon, 26 May 2025 22:24:23 -0300 Subject: [PATCH 7/7] Fix syntax error in test file --- tests/test_string_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_string_utils.py b/tests/test_string_utils.py index 5ea0062d..602313f4 100644 --- a/tests/test_string_utils.py +++ b/tests/test_string_utils.py @@ -35,4 +35,4 @@ def test_reverse_string_invalid_input(): reverse_string(None) with pytest.raises(TypeError, match="Input must be a string"): - reverse_string(["hello"])) \ No newline at end of file + reverse_string(["hello"]) \ No newline at end of file