From fbce7c176e8f9fb353d7cbf3c6cea65d62bf60e3 Mon Sep 17 00:00:00 2001 From: testtesting123987654 Date: Mon, 26 May 2025 22:15:36 -0300 Subject: [PATCH 1/5] Start draft PR From 70d57206f1bacb3a0e065749c67b7a64cdcf53b0 Mon Sep 17 00:00:00 2001 From: testtesting123987654 Date: Mon, 26 May 2025 22:15:48 -0300 Subject: [PATCH 2/5] Add 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..09629f5e --- /dev/null +++ b/src/string_utils.py @@ -0,0 +1,19 @@ +def reverse_string(input_string: str) -> str: + """ + Reverses 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") + + # Reverse the string using slice notation + return input_string[::-1] \ No newline at end of file From 306df13f9d2350000621194694cd8e97e1735c05 Mon Sep 17 00:00:00 2001 From: testtesting123987654 Date: Mon, 26 May 2025 22:15:56 -0300 Subject: [PATCH 3/5] 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..74b6dca0 --- /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("world") == "dlrow" + +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.""" + 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("Hello, World!") == "!dlroW ,olleH" + +def test_reverse_string_with_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(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 813d510bad0e5d44fbba91f366350ea1ecc87031 Mon Sep 17 00:00:00 2001 From: testtesting123987654 Date: Mon, 26 May 2025 22:16:40 -0300 Subject: [PATCH 4/5] Reimplement string reversal using two-pointer technique --- src/__pycache__/string_utils.cpython-310.pyc | Bin 0 -> 645 bytes src/string_utils.py | 20 +++++++++++++++--- ..._string_utils.cpython-310-pytest-8.3.5.pyc | Bin 0 -> 2990 bytes 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 src/__pycache__/string_utils.cpython-310.pyc create mode 100644 tests/__pycache__/test_string_utils.cpython-310-pytest-8.3.5.pyc diff --git a/src/__pycache__/string_utils.cpython-310.pyc b/src/__pycache__/string_utils.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c1b5dd98d26432e8f6d994f524b1238cdb02242a GIT binary patch literal 645 zcmY*WO-~y!5Vdy~nyg9=Jw)owNA?hw3l~&XtqO;z5=a3qT%t`p*;q~NXzb-9;#Arn z)4#N*N}Tu$Twv@CA&lhtu;;yb8k?P+l#v~MI+*_Fj6L{b(-JDa(Xm}}k^z$(*d&pW zjQ=E;Tqb8M+p0e*y{O#KI#jx>w-H>0dQ5z4^SS5xLB|Smj@>cG?<3oGk>vOMH@{=V zO1yvL&x}};Wt|GL0bBuEu)nl+W3CrpirdhZRYvy0*(<4f%=mmwvnl<(8h^kjrIbU-M-(L zm{-<$Ew8U9kI!UTT&Kz^ZC#j((f}7=EDeROVANhrFB?%R)~8^Wz%_ zT7v9dj4JJ_{O4)EpHHBmKY`w;QZq?S*9L!4FgPxHMG4yF2n%C_AMWQC$9c1?Vdd1! z?k#TX&T0> str: """ - Reverses the given input string. + Reverses the given input string using a manual character-by-character approach. Args: input_string (str): The string to be reversed. @@ -15,5 +15,19 @@ def reverse_string(input_string: str) -> str: if not isinstance(input_string, str): raise TypeError("Input must be a string") - # Reverse the string using slice notation - 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) + + # Use two-pointer technique to swap characters + 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 + return ''.join(chars) \ No newline at end of file diff --git a/tests/__pycache__/test_string_utils.cpython-310-pytest-8.3.5.pyc b/tests/__pycache__/test_string_utils.cpython-310-pytest-8.3.5.pyc new file mode 100644 index 0000000000000000000000000000000000000000..10cbcf9a92ed0c7fb09a073a1e3e5d8cf8f155ca GIT binary patch literal 2990 zcmd5;&2Jk;6rb4-d+pe6S|B2Ph)r5b4T+tEmQtxfMNrfV6b>zl$YnM0OyX_VyJlvb zG{!!p;fgqvV-LwO^w9rjuQ_qxRE7WkSWm|-jmb2p0Y$UgmgApsrx4ab(bT)T>Au9#$ z@*MCY&vOs>7%%W5@Dd;6CE#UV<`v)-eu|F+KgH=SQa$}AoJW?bG_6RnA*2$4(lU(K z8>oNw%Yl%G_7yaBl>$QACz|xg4((HJ^{pQ5S$D0z-Lus+F#YqRzs&tH~af7eLCP6jK-nxd*c0bil1W%5x8$3DS4as-6>4kX@aJSzgcGqdW9I z^xqMh*N(rgW23M*Il~ zg9eMDK#I0(B&{}+A}zsiu-XYD9mXonMG4GM>Dz&7CY^|zslj@btg&(yX6FCKbIgIPgq$TR>P1a>G0IFZVr$x*KpZ#IA6YEa>+wX7Qtgi{CJ5tP|fgC1* zgYE)LWP{DJbrI`2Sdu71gSk5TNf|mwqM)P0NY&a~>7?=D@e~V8063UaNFG|dRGtOl z#MFDT3!}$Uj~)x}EcGmtdiGK3Ie(LS4u<;3)N==^m*e>pQqOxK^*jjgLU+o9_p^xh zlQo~kzG$`e)*#wzDUnbGiNvB};!Judm623{EXmU-zJmn6S4O5m8tqB5c>#u>VAStg z@;tB;{V31DDBwKzGEQGX?TdupXfnwfTF6>T*=u9{VANO7P6+S>WA8@*Mpj>i_vP>Y ze=E6I)`}ziIVf(Mp>F!B%^E`0x?`rk`KH)($4s&DO|AQP-Hc}j*_kGmjGI4sm7C?q z7#79u+)H6`+zinOS@b+Yz@+jGEZH;uG8WLOZg~o=TJ~qqtQ)Uw*7;GXmW>Yi^HqrSL?SDCEDip4y9z7aL0jH!j4Tz?9CH3u?brI z{|IfwDre-X`4d-F%vtU_yh)Cq#+_RP&RZH@iMnd8d(lLiZZhrP-f9bT5Bj02Y`fN1AD|1*uZSY3bNpso#o#xD@ zDlHtHloj6R<<&^0#!ij;B4ND2qL2p$#{00jIs;_fD$@6!j@ub6(dnmh0S_Key=Tt? z`6ejR$shw#%kFZx+-p*7%230j;7FC`2Rey+%Q`L`SF%ydT#((T8Sb+<0n5Cpof0!! nVgg8v5rbjq=6G(JceR!9P9(mdFwIynO-j_WJlAuwf64g^`p5tj literal 0 HcmV?d00001 From ea475b6459560ec4987445fb91049f5cf442d07c Mon Sep 17 00:00:00 2001 From: testtesting123987654 Date: Mon, 26 May 2025 22:16:48 -0300 Subject: [PATCH 5/5] Update tests to cover more string reversal scenarios --- tests/test_string_utils.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_string_utils.py b/tests/test_string_utils.py index 74b6dca0..f5bf4f56 100644 --- a/tests/test_string_utils.py +++ b/tests/test_string_utils.py @@ -26,6 +26,10 @@ def test_reverse_string_with_unicode(): """Test reversal of string with unicode characters.""" assert reverse_string("こんにちは") == "はちにんこ" +def test_reverse_string_with_numbers(): + """Test reversal of string with numbers.""" + assert reverse_string("123 abc") == "cba 321" + 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"):