From b866f1ca6b22bf74258f13e8c90d67e03900c200 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Tue, 20 May 2025 20:18:24 +0000 Subject: [PATCH 1/5] Start draft PR From 9fb40c897c0cac351e6d0e0327b9506ace732e49 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Tue, 20 May 2025 20:18:46 +0000 Subject: [PATCH 2/5] Add string reversal function with input validation --- 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..ca3c8c0 --- /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 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 1d8ada725645869549e5589bf2fefefc14a71289 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Tue, 20 May 2025 20:18:55 +0000 Subject: [PATCH 3/5] Add comprehensive tests for string reversal function --- tests/test_string_reversal.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 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..c2f6a15 --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,27 @@ +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_special_chars(): + """Test reversing string with special characters.""" + assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" + +def test_reverse_string_single_char(): + """Test reversing a single character string.""" + assert reverse_string("a") == "a" + +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) \ No newline at end of file From e0906d18fff84bfbfccb2c0c068d6d955b97cc55 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Tue, 20 May 2025 20:19:34 +0000 Subject: [PATCH 4/5] Implement string reversal without using slice or reverse methods --- .../string_reversal.cpython-312.pyc | Bin 0 -> 626 bytes src/string_reversal.py | 22 ++++++++++++++---- ...ring_reversal.cpython-312-pytest-8.3.5.pyc | Bin 0 -> 4978 bytes 3 files changed, 18 insertions(+), 4 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..ab89b5ba712b4b49e6ba4afb46a1da091266e845 GIT binary patch literal 626 zcmY*Xze~eF6uwKM^hfOApcSkfP7R9DwTK`h=;Ba2xs=$PHBggqcNJP3M09skcM-(@ z!GFM|qEm3|@th)~4}-;{F1i4x=If literal 0 HcmV?d00001 diff --git a/src/string_reversal.py b/src/string_reversal.py index ca3c8c0..b5749e9 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 using manual character iteration. Args: input_string (str): The string to be reversed. @@ -11,9 +11,23 @@ def reverse_string(input_string): Raises: TypeError: If the input is not a string. """ - # Check if input is a string + # Validate 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 + # Handle empty string case + if len(input_string) <= 1: + return input_string + + # Convert string to list for manipulation + chars = list(input_string) + + # Swap characters from start to end + left, right = 0, len(chars) - 1 + while left < right: + 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 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..e0c49d79e572dcfc5d9e6e33b505b98bd4625340 GIT binary patch literal 4978 zcmeHL&2JM&6rZujpPPiQmX?o{O^hLKh@FHOirOGjsfS*o9_WEwRvYgocDnx1%!b4v zkx=1M38_6)BC0s}NR&$b8#q?I*l|9pRjXE_7jB4DLdvOcW_H&mgd#pvQHk;H+uwWp z-kb5vym>SGOJ`>sLHX(anfwnygzj_3D*+d5e*xe&GLV5SG=ar4Pz>e*7!}Dx2+KF& zi7>ngSdn6MA_}tLO*9cR2++6@0;(8cpdCg8XlE6tq7S(H@f3EtsCm_-%+#1o3#I9t zTdjCRfI_>_b`2=B0WTpNdkTwa)x}HL2rLB_@nYbsK7^Kni$V6Y&Fw^)As%Wy4bYjw z2H`eSfmK(-2-&|cwzN31eb#FuZMW#~U;E%`ixP(RwEo2& zHmx3bjvn?}@f^K?r)PxkkcBYxB{3pKbdeOW;6nC3ufc7e_It9{b1~$3@}6H~BQ_7? z2RDX0xD+tr&m2wV?P$uroY`yC-^kId#!?*%$1a=9Rws2<$f+`%RTCQ4># zkU_X2Rg|4fMz2&d(3Ub2R2$6jf64@P7h3oSeBFhSsZ^aKq9dMMi8V2f^dz|D>mwtu z+FnP09_^`9<6Tt5sd%`<~-3;O`*$p5(_Kw#{0;Ya%Q|Sg&I|!t%bk`0xl*AU{ z*M=*QoKG9vOmfW@TqlX;UMXD@c0R5h^i6%RNfI;(?Rd$l|8|E4eaFlHDyPR6(z~X< zo7d+Gc3x!_Ggr_pHJ8_^p0gpHq!(iP(ByFLY|o{{LM(Bq=WK3xa!7yq=;+xiGr9F@ zyk15>gih^*+mDiDon#?O`kTrrKzI9r)Rj|r`x{Dji|}j16-dsf4Q?j6W(%&9>~g=9 zt_eFI-*v^l0|1*O`zlfLHbL+Zf%2N7kh!}#{%SuB*a3ZseAya20Nx?UHPDvXeh}o!mkZiAUU5lxS8adEx1mS%aCTc z-I}oTam^J=fux%x`E)GsZ7R=@+Yf=?7&vv2@8MBiTRbtgA)=XB6l8puC0aARoyBWny8s(!R@M zM}NikecSy__xII$-=)pi*l*Fp-?8=RvByCa{}lfnM)Cc-U)$@^+3EopfW2wl!Goi6;B-Q0ay(1Vs40_HHO&;3P^4tUmsWdg` zXu*a9m@jf7u8h6|Hgt#^9%3|?mYwFGeB@5;B-i0PEs;n>1c|>*N4Sjdcw}!yKPi@t zS Date: Tue, 20 May 2025 20:19:44 +0000 Subject: [PATCH 5/5] Update tests for comprehensive string reversal coverage --- tests/test_string_reversal.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py index c2f6a15..61eb7db 100644 --- a/tests/test_string_reversal.py +++ b/tests/test_string_reversal.py @@ -10,6 +10,10 @@ def test_reverse_string_empty(): """Test reversing an empty string.""" assert reverse_string("") == "" +def test_reverse_string_with_spaces(): + """Test reversing string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + def test_reverse_string_special_chars(): """Test reversing string with special characters.""" assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" @@ -18,6 +22,10 @@ def test_reverse_string_single_char(): """Test reversing a single character string.""" assert reverse_string("a") == "a" +def test_reverse_string_mixed_characters(): + """Test reversing string with mixed character types.""" + assert reverse_string("Hello, World! 123") == "321 !dlroW ,olleH" + 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"):