From 915210441d6f975dfe9b340935362cb484f4d11a Mon Sep 17 00:00:00 2001 From: momstrosity Date: Tue, 20 May 2025 20:01:22 +0000 Subject: [PATCH 1/4] Start draft PR From 365f8733906503ea7116eb9b6143a89577a28245 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Tue, 20 May 2025 20:01:41 +0000 Subject: [PATCH 2/4] Add string reversal function with type checking --- 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..84f031b --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,19 @@ +def reverse_string(input_string): + """ + Reverse a given string. + + Args: + input_string (str): The string to be reversed. + + Returns: + str: The reversed string. + + Raises: + TypeError: If 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 4865e2b5fb3f97433490d6c2ba8565aacebe75c4 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Tue, 20 May 2025 20:01:50 +0000 Subject: [PATCH 3/4] Add comprehensive tests for string reversal function --- tests/test_string_reversal.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 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..aa55c2f --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,33 @@ +import pytest +from src.string_reversal import reverse_string + +def test_reverse_normal_string(): + """Test reversing a normal string.""" + assert reverse_string("hello") == "olleh" + +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_with_spaces(): + """Test reversing a string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_with_special_chars(): + """Test reversing a string with special characters.""" + assert reverse_string("a!b@c#") == "#c@b!a" + +def test_non_string_input(): + """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 From 80acad44691f8d71164b351dc06fbc47292e6df5 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Tue, 20 May 2025 20:02:26 +0000 Subject: [PATCH 4/4] Implement string reversal without slice notation or reverse() --- .../string_reversal.cpython-312.pyc | Bin 0 -> 634 bytes src/string_reversal.py | 19 +++++++++++++++--- ...ring_reversal.cpython-312-pytest-8.3.5.pyc | Bin 0 -> 5347 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..e6f7589f653e25997a701382e1193b687dd5248f GIT binary patch literal 634 zcmZWny-UMD6u)bt)P4;PRl&;P)Sy^hMFbH+mkza)ONq@{18oxSE<&Y)i0*E!yAI<2 z;o?%!#UMC1xfSZ-W~Ut&u0+C?fdfa14igW7I8qWVk{|rWT6Xm zfV$X7HDg4=It&fQco$M1i9f7|L<*Kv+($E>6%+BAh3_NoIqo4V1)p}Q%>lfR4E^*q zLm+DOgfdQvNm}*^bqE*Cc3LY6Q)!E}__~@&y6t$rXoMtEhQF~+>c=#a5#f?UN?7P= zC0uG$_{>QdfizUcPbd%+vRNfLCX&B>iY8o#n?k{g>qf7t* literal 0 HcmV?d00001 diff --git a/src/string_reversal.py b/src/string_reversal.py index 84f031b..5981f1c 100644 --- a/src/string_reversal.py +++ b/src/string_reversal.py @@ -1,6 +1,6 @@ def reverse_string(input_string): """ - Reverse a given string. + Reverse a given 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): 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 for manual reversal + chars = list(input_string) + + # Manual reversal using two-pointer technique + 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 and return + 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..a703d5c10819ec1702fff194f44bfe33efcf1e93 GIT binary patch literal 5347 zcmeHL&2JM&6rZujpPN7kEg~p^O-#U68$02%K@AF#TB!#((MsfEZM>V<>H0%68xp69 zgbJ5RNaaweIdJfiC{_OlxK+K_aXzF~D=M{@-YQZFDW|@f*&UmNik5x|wT!)QfA7t_ znc10nZ)V=Not<$6>FdXD=RYI}{m2%cJ>sa&zFCzz#OiO6Z!^_wVEC-hGQsCnxLd(IWAp6GQdLqrj_w7w4 zl$pjR;X2cSHLrvja(Ig(%|?CHdarr#CSpBp^^1{gmEy=ztlvl4uF;{g71*y)!qA`A zS3F0jH3Bavhy7l>pj^N&XNK>S#W0K|F(YPli4?HlLe4?I!)-Nn`Lfn~DdgAWzdyxh zYysvE9t=-#IZ%zI;)^G)Tg=hr5`fv)4ZT#RMZ?zRYDl}Gyk*;EH&nK5E5C|&17l;W zcyV|zS*e~&vG^w*(8qL+hEpub-)B-R6<^lJr1WBnQ9h8X#0^3jPR?+0j+2YK2Moq6 z>WB(PQ;Z8oudT*}iW?uVR2}GJ)}Zd8Y|gOlEVU{$S1wizYPlVdot>I1*iNCu+^}7S z63jiCW%=@)ZHl7V8M{1X*x8a%wA@&h8PgW8!98$AG+QW_-lMck-NV^w{^?HETBz7Y z$#5WNHSB7<1=+)qcIIq5bD^C%FBw_eJF<(C85w3U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