From 064e621d9b3ebc478a3a9c541e6f7492884dd0f9 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Thu, 22 May 2025 12:40:47 -0300 Subject: [PATCH 1/4] Start draft PR From 0c1ef260a52ff7b7f5afe9a4babdd157d83a6ea4 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Thu, 22 May 2025 12:41:05 -0300 Subject: [PATCH 2/4] Add string reversal function with type checking and docstring --- src/string_reversal.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/string_reversal.py diff --git a/src/string_reversal.py b/src/string_reversal.py new file mode 100644 index 0000000..2ea4487 --- /dev/null +++ b/src/string_reversal.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 reversed string + return input_string[::-1] \ No newline at end of file From e77320571e80d277175f739bcc21cad28f279798 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Thu, 22 May 2025 12:41:14 -0300 Subject: [PATCH 3/4] Add comprehensive tests for string reversal function --- tests/test_string_reversal.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/test_string_reversal.py diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py new file mode 100644 index 0000000..18fe2bc --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,34 @@ +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 reversing an empty string.""" + assert reverse_string("") == "" + +def test_reverse_string_single_char(): + """Test reversing a single character string.""" + assert reverse_string("a") == "a" + +def test_reverse_string_with_spaces(): + """Test reversing a string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_string_with_special_chars(): + """Test reversing a string with special characters.""" + assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" + +def test_reverse_string_unicode(): + """Test reversing a string with Unicode characters.""" + assert reverse_string("こんにちは") == "はちにんこ" + +def test_reverse_string_invalid_input(): + """Test that TypeError is raised for non-string input.""" + 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) \ No newline at end of file From 5f382b09df7b1261d9b0f80cc69d1aab6ae10afb Mon Sep 17 00:00:00 2001 From: momstrosity Date: Thu, 22 May 2025 12:41:52 -0300 Subject: [PATCH 4/4] Implement string reversal using two-pointer manual swap technique --- .../string_reversal.cpython-312.pyc | Bin 0 -> 745 bytes src/string_reversal.py | 19 +++++++++++++++--- ...ring_reversal.cpython-312-pytest-8.3.5.pyc | Bin 0 -> 6705 bytes 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 src/__pycache__/string_reversal.cpython-312.pyc create mode 100644 tests/__pycache__/test_string_reversal.cpython-312-pytest-8.3.5.pyc diff --git a/src/__pycache__/string_reversal.cpython-312.pyc b/src/__pycache__/string_reversal.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..dd89b21edff262aa13e290c142cdc47aa5f74c9f GIT binary patch literal 745 zcmY*Xy=xRf6rcStCihh&A)a7lu*xFIBy9+SNWe5H&QvzX-tD`+#l79-&1~dC3K8wK zO6)8wl>8}zoM_`>XJvPAsciJkejMjp=DjyFzu%kRyq(X@W{r`(Jru()24lbUq%ylq zX6uw(FpmNA3}6G>Ghf+Hjj`p}StE#&RCT4oAo8;%1eIdcr%H3vRV*5_$K)=Un(v)6 zWmc%GGLv<6qgwNfs}lRtIA@+opY__hP1{eHW1sz~ak37a0!rX&2;2`&A>z3pUI^N$ z<)DY?%l$mlltsmSi-68PKOuTS;VR}m;8@i4$_kzQUJFGCT(BzO|0j0bK*9q2WSqbu zVoao?K`yqC8Au+*io0{R7I?OCq?^h|sZ?4^wR`;gFPURywLk`uRBqIV%pxV(%Dlc~ zpl+qqo#TH-Vi=D=gl>wi@B^jCFzty1X_;D}{(jfEz#q$`SQHZhcZo582 zN+^&@3WOvse0Nt!?2DpZ-QxakxRZ>tR@sd5IiP-8T8O+)?ghKP!x|fJ!VizW*SCJQ zZcW;|-&%W<<-O?=Yqa0lm-jC1ul3D|y*VQ|J9bRGNinbTiD6ymE9A1}gHi0IAw0k) dP4ffVluRwdFsA0Jargb&lu`J&cCACs{0F5n%KZQU literal 0 HcmV?d00001 diff --git a/src/string_reversal.py b/src/string_reversal.py index 2ea4487..29ab24f 100644 --- a/src/string_reversal.py +++ b/src/string_reversal.py @@ -1,6 +1,6 @@ def reverse_string(input_string: str) -> str: """ - Reverse the given input string. + Reverse the given input string using a manual character-by-character approach. 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 reversed string - return input_string[::-1] \ No newline at end of file + # Convert string to a list of characters + chars = list(input_string) + + # Use two-pointer technique to swap characters + left, right = 0, len(chars) - 1 + while left < right: + # Swap characters + chars[left], chars[right] = chars[right], chars[left] + + # Move pointers + left += 1 + right -= 1 + + # Convert back to string + return ''.join(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 0000000000000000000000000000000000000000..34a113f7f89dcfd0fdd3de6faa52c16ffaa69e2b GIT binary patch literal 6705 zcmeHL&2JmW72hF;6v>tB#BSaCL&@5-Y!R|Z%8HVxs9iHa4n5fD!97GMuvl?dBAq3d zm|0pPBLQ)eO8^5c-~thf9N3!#70BPX;dnOueWgO|V?U5}WluboZxcsAu&-WxXS-^srP(ByP+YK&+tk7 zUeEAMYF)b_rEg?7LzCE|UKGk8Tt|2f;f<#!49+d)iVEg49EJ0Dym+nQmYk{=uR0~S zQQJz2D!kM!kVAiUgLyCOMZ>mrX4P2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