From e16387d8571c413423f8f5f5a02e23b9591161f9 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Tue, 20 May 2025 17:32:49 +0000 Subject: [PATCH 1/5] Start draft PR From ce98de47578bc2ce4cadcddb3f55ededc177caa2 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Tue, 20 May 2025 17:33:13 +0000 Subject: [PATCH 2/5] 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..65fb138 --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,19 @@ +def reverse_string(input_str): + """ + Reverse a given string. + + Args: + input_str (str): The input 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_str, str): + raise TypeError("Input must be a string") + + # Return the reversed string + return input_str[::-1] \ No newline at end of file From e80d8e9bb77b226e5dd94c2295cbe098c939df06 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Tue, 20 May 2025 17:33:20 +0000 Subject: [PATCH 3/5] Add comprehensive tests for string reversal function --- tests/test_string_reversal.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 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..86c7336 --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,35 @@ +import pytest +from src.string_reversal import reverse_string + +def test_basic_string_reversal(): + """Test basic string reversal""" + assert reverse_string("hello") == "olleh" + assert reverse_string("python") == "nohtyp" + +def test_empty_string(): + """Test reversal of an empty string""" + assert reverse_string("") == "" + +def test_single_character(): + """Test reversal of a single character""" + assert reverse_string("a") == "a" + +def test_string_with_spaces(): + """Test reversal of string with spaces""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_string_with_special_characters(): + """Test reversal of string with special characters""" + assert reverse_string("!@#$%") == "%$#@!" + +def test_unicode_string(): + """Test reversal of unicode string""" + assert reverse_string("こんにちは") == "はちにんこ" + +def test_invalid_input_type(): + """Test that 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) \ No newline at end of file From 97d86e281658ee8fb51ea72bf1bb9feac801e33c Mon Sep 17 00:00:00 2001 From: momstrosity Date: Tue, 20 May 2025 17:34:06 +0000 Subject: [PATCH 4/5] Update string reversal function with explicit type checking and comments --- src/__pycache__/string_reversal.cpython-312.pyc | Bin 0 -> 637 bytes src/string_reversal.py | 4 ++-- ..._string_reversal.cpython-312-pytest-8.3.5.pyc | Bin 0 -> 6562 bytes 3 files changed, 2 insertions(+), 2 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..1328a2a33a9ea38e35bc50c8737ab9b9e96aa60a GIT binary patch literal 637 zcmZWny-UMD6u)b#w6%6n{C2oSFe0{Hizp6)E)KPmONq_d25J)SE`^p3B09L$$$!8_ z{1+VETnf4v90VsfAxEo+(%{#KJ8Ll0QeB; z>E3&eKvZdmGERv>npTI}gbQZb%?*XAw8NUbqIQyQ*{&z*Kq9M<^a`o9Xm~kN65)_z zN?2%XCXA|7c+5_CfkCK`$p4F14U5x+q}FxmE@KV|_fLq3Yg?Syj({eT&+pCd%h!@q zj|=&x2tD}xh0rxWXK~Bs!mu0E*FfoKV}Bj{*|=9O$4Guwi-zkK8Fd}5a=ldKtWgY0 z*25-@_J-T_3!{$YpkSIT3wr6n*C3st?`4#q9ay*1FB2A6rIX3AgVC_i&u y8?Zs`YI~So41kRebgcAuh{@lCWgz*P%~Qwp+H{NM;i#-7_@!ZtgVYpWQrthjT%`K| literal 0 HcmV?d00001 diff --git a/src/string_reversal.py b/src/string_reversal.py index 65fb138..cd32191 100644 --- a/src/string_reversal.py +++ b/src/string_reversal.py @@ -11,9 +11,9 @@ def reverse_string(input_str): Raises: TypeError: If the input is not a string. """ - # Check if input is a string + # Validate input is a string if not isinstance(input_str, str): raise TypeError("Input must be a string") - # Return the reversed string + # Return the reversed string using slice notation return input_str[::-1] \ 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..a7feff4a14038fa3b216f1cf563ef6d03074b00c GIT binary patch literal 6562 zcmeHL&2QUA7UxK#Pult1BpV~nGLf~hkl0ebn!0F$D8L@}keu2>jRFKC(w4H6D9q5Y zrL}>i?H&pQXn_={4?U>27%tF%pqCtry-1cn7Q_}?z)ZvW;C2PAMfvjgJ}iIw_lwr|BWijCNe>Z_+aZ@0G}$RViH@KB;pyVMoST* zROw`l$T#uHIJ}A2iE45(39{%1%4EuU!mth*NDmd;&PsZE#sCWuD@XcX?P7EovpSx{UOD9kI%K3*VZWFaz7<|BU@Rg{J3 ze3XCfqMayr;9qVpCg@BNlcLR3WZBm+V=mSx%AI6@de3Da-c|VVKL4UWds>LxkPX_% z`Yi^+*CE(xQ6tcv-oJQ`O|J)DqDO*OyhJY&=$Y})XfqCdNzH_roTn8cxR|>?XmFpW z1A(mfoR0;bg6Dh0OwGafgPXt|U5J?J7k*83?`z7wyz{)T{;T|&jZ|)+IrNsrUG28P zDvTetUIks(;m&mio)vHUSS;&so8<%oC{Z}7oS2aY193RE!g&fbN z7qqKVdL_phM#K{JqM(E10+LHet~}dkaBeYIIGD(B6i(do;`N4Gc4}U{=9Jw=eJLee zcD-vI(1W+HD4J1mYJXzPVcubV%3)Q*)vdX@ZPW}G-YmsE6>E?!KHtw= z>}OuDF{d9owLIt@#4Ug|vfa zlI!;1Dji!qBc&_CF2t?r&@=?#`X573Z5|ph+Ep`vT5~|!YNj>UQS%!VuQgvFxsZ0y zOmf{GT&4NNQ7K&!b|G%fg{C1`r}-`D45fr5DB&`-0$ubUmnrg!6I(oP z!f+I@6cU`G814!fE|(0~$6M?$!V^IHB9&!;_YR8a6W;vjW3Y`;&=r3l2nOAWHqF48 zKiyS_0ksYRX{*DnLmf4>v9u;>>mV+N}t?5Vw4>6iB*Gv;WQ5 z-#eDThs5$fwUJFXcaD93J3Cu(%Nnm6C5tyzv7^;y9cG(N)wG#2tBKw1?nC033<+@( z*0C;K*2Tu44-di}E(mAZbOM56tgDU!dUO*=TOECLv!hOIP`uWBf#gEkK{Ls9dvKLb zEW*|e?N)?ch#%dQu%E8eiM^n}Q5|kr>E^i+FGd{KI3?|+z&G3gBB-Ue)aw?T869vq(Q;`OP zB#;*u?zoo<>yRlmMSKu>B9+^aO6{&;S4>^hq(kb=Vy{Dw55vvh269jNcWVDf!ygWR z(rAxfUr!bPnLPUO%xZG@X;ewyAV0*F^nvZK-PPm?`MRAdK24w=fM-uZS0a84JbyM& z)S{2of#SL7V=Y$9B_5xP6)z`p2fSpEl4D0f&pWQmB;L5pqd{VAoLRH1;zB|$6nP0> z#(oDj>=GIt=BzX>dos*~$#eS%>frfX97M#%ODrx7!$@=hyMhQ$#Im=NZ&n?1#2yg&GjGX8`{N#RL4LPnk>Bjk@r-as;rud7`Xk)> By8i$G literal 0 HcmV?d00001 From e5ab74cc02bdb884cf247563c9fd72ed4e28af87 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Tue, 20 May 2025 17:34:42 +0000 Subject: [PATCH 5/5] Implement string reversal using two-pointer swap method --- .../string_reversal.cpython-312.pyc | Bin 637 -> 637 bytes src/string_reversal.py | 17 ++++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/__pycache__/string_reversal.cpython-312.pyc b/src/__pycache__/string_reversal.cpython-312.pyc index 1328a2a33a9ea38e35bc50c8737ab9b9e96aa60a..0bbf0020386c8cdaa0b2230f810292cbfa1f521f 100644 GIT binary patch delta 22 ccmey%@|T78G%qg~0}$}-)ycTIk+*;e08G#ZX8-^I delta 22 ccmey%@|T78G%qg~0}$NYqmyxDBX0o{08@eo8~^|S diff --git a/src/string_reversal.py b/src/string_reversal.py index cd32191..02bc2b3 100644 --- a/src/string_reversal.py +++ b/src/string_reversal.py @@ -1,6 +1,6 @@ def reverse_string(input_str): """ - Reverse a given string. + Reverse a given string manually, without using slice notation or reverse(). Args: input_str (str): The input string to be reversed. @@ -15,5 +15,16 @@ def reverse_string(input_str): if not isinstance(input_str, str): raise TypeError("Input must be a string") - # Return the reversed string using slice notation - return input_str[::-1] \ No newline at end of file + # Convert string to list of characters for easier manipulation + chars = list(input_str) + + # 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 back to string and return + return ''.join(chars) \ No newline at end of file