From bfd823b5298d4ac2b522ed1e9a1f7f9964e63802 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Thu, 8 May 2025 20:19:57 +0000 Subject: [PATCH 1/9] Start draft PR From 7b797d21b83d5834848d358ca23a4e4fc0892a05 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Thu, 8 May 2025 20:20:07 +0000 Subject: [PATCH 2/9] Add string reversal function with type checking --- 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 0000000..567d54d --- /dev/null +++ b/src/string_utils.py @@ -0,0 +1,19 @@ +def reverse_string(input_string): + """ + Reverse the 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 cb9b7ae6486e260f4407fc79e09895967719d634 Mon Sep 17 00:00:00 2001 From: momstrosity Date: Thu, 8 May 2025 20:20:14 +0000 Subject: [PATCH 3/9] Add comprehensive tests for string reversal function --- tests/test_string_utils.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 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 0000000..63ba350 --- /dev/null +++ b/tests/test_string_utils.py @@ -0,0 +1,37 @@ +import pytest +from src.string_utils import reverse_string + +def test_reverse_standard_string(): + """Test reversing a standard ASCII string.""" + assert reverse_string("hello") == "olleh" + assert reverse_string("python") == "nohtyp" + +def test_reverse_empty_string(): + """Test reversing an empty string.""" + assert reverse_string("") == "" + +def test_reverse_unicode_string(): + """Test reversing a string with unicode characters.""" + assert reverse_string("こんにちは") == "はちにんこ" + assert reverse_string("Hello, 世界!") == "!界世 ,olleH" + +def test_reverse_palindrome(): + """Test reversing a palindrome.""" + assert reverse_string("racecar") == "racecar" + assert reverse_string("12321") == "12321" + +def test_reverse_with_spaces_and_punctuation(): + """Test reversing strings with spaces and punctuation.""" + assert reverse_string("hello world") == "dlrow olleh" + assert reverse_string("a,b,c") == "c,b,a" + +def test_invalid_input_type(): + """Test that non-string inputs raise a TypeError.""" + with pytest.raises(TypeError): + reverse_string(12345) + + with pytest.raises(TypeError): + reverse_string(None) + + with pytest.raises(TypeError): + reverse_string(["hello"]) \ No newline at end of file From 9896ac25646d67f96157e36724e70835797362cb Mon Sep 17 00:00:00 2001 From: momstrosity Date: Thu, 8 May 2025 20:20:54 +0000 Subject: [PATCH 4/9] Implement manual string reversal algorithm --- src/__pycache__/string_utils.cpython-312.pyc | Bin 0 -> 625 bytes src/string_utils.py | 21 +++++++++++++++--- ..._string_utils.cpython-312-pytest-8.3.5.pyc | Bin 0 -> 7773 bytes 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 src/__pycache__/string_utils.cpython-312.pyc create mode 100644 tests/__pycache__/test_string_utils.cpython-312-pytest-8.3.5.pyc diff --git a/src/__pycache__/string_utils.cpython-312.pyc b/src/__pycache__/string_utils.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..551f20b3b4dcbcd30ed736366233f29ef3a8b7c3 GIT binary patch literal 625 zcmY*Xze~eF6uxVs)cyh;Y6UBYAT$VVT}1>DK^KSG$)(ihYy)i)?kJ;3Z+zJ&07kzh0O8vsU+`aF6U*0?Nl1?WO;CnIOdd3KS$U_7;D@>%Mk4$uf zPEijVu|~v5T8E{!unrHoS|T9h=cF0~04%(9!sgrfIYlW(h?6kE3IiCQ2a1$Z0VqzZ*$MmXe*5*9d` zp+kiVkJ&ROAP%Y`;r}mI>K3On@@mhe2aGvTbabxTR9z5@6WbA_KCPAwUSyACi=^Xm zA$y6~kqCfxu literal 0 HcmV?d00001 diff --git a/src/string_utils.py b/src/string_utils.py index 567d54d..9cb330e 100644 --- a/src/string_utils.py +++ b/src/string_utils.py @@ -1,6 +1,6 @@ def reverse_string(input_string): """ - Reverse the given string. + Reverse the given string using a manual character-by-character reversal algorithm. Args: input_string (str): The string to be reversed. @@ -15,5 +15,20 @@ 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 = 0 + right = len(chars) - 1 + + while left < right: + # Swap characters + chars[left], chars[right] = chars[right], chars[left] + + # Move pointers towards center + 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_utils.cpython-312-pytest-8.3.5.pyc b/tests/__pycache__/test_string_utils.cpython-312-pytest-8.3.5.pyc new file mode 100644 index 0000000000000000000000000000000000000000..102a9f5180f03ed3157b6e738a5f26f2aff4872f GIT binary patch literal 7773 zcmeHMO>7&-72f3zf0irBj$JslDj_RTfkjIqDcMdcAhsg41(Jh(X;2_SfyJ7;7U?dz z?CjDKSqX@fTnZRy0QW~IdSGu7bjY>W_*V2nij-w6610GV7QGc#0Rui1eKWH=OZ`Mb z8W(i|4tL*t@6Eio!`YoTZ{Pl@zdtEJ`PVDMg})*}*dt6(B0kvv5rE$ahG2-6uqe_x zQj+o!Q7ECssK{Dki!o@4Sn*O~F#$5^V_{J?5YVI%1*#Y^pnXOhXupvFI#3hS^54lt zZly(U0Gszr?3kM4;$nFz?_Zo&ai9{M`wgHl5^+s%#h}Bgu;t@5(TJ=?R>jrGPsRmd zOu(4i^LZ+z|~#bnZm9{2oTsC0C^7ZX!Toz2(Duf^)IUKAp+17eOv( zgE8W6jXvJY!BLH}2;&L=#W6bJ44l$W3`TKEzev!}i2V-LV=$MVkE!vZYsUI$>#Fzad-Wtyqh&S-B-W#p6R$M zThOqURb6#ly=>^%Q0IU6y?5UQmaOrN7cH2UWqVQEvdqG!_&jp``leXVy*^&4T}?a5 zpZP$&t`d|>JL9CDNjvG}ntGi{-%LB0ERRj(NzjLo8A7fS^5*ja-EmCpQUkMThX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ut|XA1|*(m^_W@8M>U@g6^tiyFA5KlUgiSs;;AA zz37;#uD(~Rm^2}tDbB+4dkaTl*3yXP=OH`o#b`&zlX*v$>lf&D%aUa*Mk$u>K>%cE z^a2{>^iBK**rMsRp91(m_)QSK zbbQl#{F7QIF+v-g^2oD((1-f@Gq77RbnB?2uUR tACf4Fe-?y~g^&8b5GI}?Nz6S>M#QnF2}yige43QR3?=(XSsbPI{tY*@Yh(Zb literal 0 HcmV?d00001 From a4f61b9cef9acb3259d5f5c60049cc8c91262c38 Mon Sep 17 00:00:00 2001 From: laura-abro Date: Thu, 8 May 2025 20:21:55 +0000 Subject: [PATCH 5/9] Start draft PR From 4cb3ffa0922b6f9a1479c1f6dc90332581bf6719 Mon Sep 17 00:00:00 2001 From: laura-abro Date: Thu, 8 May 2025 20:22:10 +0000 Subject: [PATCH 6/9] Add array flattening function with recursive implementation --- src/array_utils.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/array_utils.py diff --git a/src/array_utils.py b/src/array_utils.py new file mode 100644 index 0000000..594c283 --- /dev/null +++ b/src/array_utils.py @@ -0,0 +1,31 @@ +from typing import List, Union, Any + +def flatten_array(arr: Union[List, Any]) -> List: + """ + Recursively flatten a nested list into a single-level list. + + Args: + arr (Union[List, Any]): Input list or nested list to be flattened. + + Returns: + List: A flattened list containing all non-list elements. + + Examples: + >>> flatten_array([1, [2, 3], [4, [5, 6]]]) + [1, 2, 3, 4, 5, 6] + >>> flatten_array([1, 2, 3]) + [1, 2, 3] + >>> flatten_array([]) + [] + """ + # Base case: if input is not a list, return it as a single-element list + if not isinstance(arr, list): + return [arr] + + # Recursive case: flatten each element and combine + flattened = [] + for item in arr: + # Recursively flatten each item and extend the result + flattened.extend(flatten_array(item)) + + return flattened \ No newline at end of file From 4c093bff8599f982570c84f3009d9a47bc24f4d6 Mon Sep 17 00:00:00 2001 From: laura-abro Date: Thu, 8 May 2025 20:22:19 +0000 Subject: [PATCH 7/9] Add comprehensive tests for array flattening function --- tests/test_array_utils.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/test_array_utils.py diff --git a/tests/test_array_utils.py b/tests/test_array_utils.py new file mode 100644 index 0000000..4e96f5f --- /dev/null +++ b/tests/test_array_utils.py @@ -0,0 +1,30 @@ +import pytest +from src.array_utils import flatten_array + +def test_flatten_simple_list(): + """Test flattening a simple list.""" + assert flatten_array([1, 2, 3]) == [1, 2, 3] + +def test_flatten_nested_list(): + """Test flattening a nested list.""" + assert flatten_array([1, [2, 3], [4, [5, 6]]]) == [1, 2, 3, 4, 5, 6] + +def test_flatten_deeply_nested_list(): + """Test flattening a deeply nested list.""" + assert flatten_array([1, [2, [3, [4]]], 5]) == [1, 2, 3, 4, 5] + +def test_flatten_empty_list(): + """Test flattening an empty list.""" + assert flatten_array([]) == [] + +def test_flatten_mixed_content(): + """Test flattening a list with mixed content types.""" + assert flatten_array([1, 'a', [2, ['b']], 3]) == [1, 'a', 2, 'b', 3] + +def test_flatten_single_item(): + """Test flattening a single item.""" + assert flatten_array(42) == [42] + +def test_flatten_nested_empty_lists(): + """Test flattening list with nested empty lists.""" + assert flatten_array([[], [1, []], 2]) == [1, 2] \ No newline at end of file From ee06130d7805e43b9ffb06a711e3062bc1dfd857 Mon Sep 17 00:00:00 2001 From: laura-abro Date: Thu, 8 May 2025 20:22:27 +0000 Subject: [PATCH 8/9] Add pytest as a requirement --- requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..55b033e --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pytest \ No newline at end of file From 11e0048bb9b4748603627323f2103f18f3e9426a Mon Sep 17 00:00:00 2001 From: laura-abro Date: Thu, 8 May 2025 20:53:58 +0000 Subject: [PATCH 9/9] Start draft PR