From c7220b8176c542cdfa75f09fb20a1d31f504ef93 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Wed, 7 May 2025 12:33:57 -0300 Subject: [PATCH 1/8] Start draft PR From 1f73929b704ecd581678d3f1135be51e09cae827 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Wed, 7 May 2025 12:34:10 -0300 Subject: [PATCH 2/8] 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 00000000..1dfb1bff --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,19 @@ +def reverse_string(input_string: str) -> str: + """ + Reverse 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") + + # Return the reversed string + return input_string[::-1] \ No newline at end of file From fc3952dfabca4d7796c6f384b104bef3741c46d6 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Wed, 7 May 2025 12:34:19 -0300 Subject: [PATCH 3/8] 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 00000000..2c1dd4af --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,33 @@ +import pytest +from src.string_reversal import reverse_string + +def test_reverse_standard_string(): + """Test reversing a standard string.""" + assert reverse_string("hello") == "olleh" + +def test_reverse_empty_string(): + """Test reversing an empty string.""" + assert reverse_string("") == "" + +def test_reverse_palindrome(): + """Test reversing a palindrome.""" + assert reverse_string("racecar") == "racecar" + +def test_reverse_with_spaces(): + """Test reversing a string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_with_special_characters(): + """Test reversing a string with special characters.""" + assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" + +def test_reverse_unicode_string(): + """Test reversing a string with unicode characters.""" + assert reverse_string("こんにちは") == "はちにんこ" + +def test_invalid_input_type(): + """Test that a TypeError is raised for non-string input.""" + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(12345) + reverse_string(None) + reverse_string(["list"]) \ No newline at end of file From 9715dca8b8a444ff40a22309d54074929d124e1c Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Wed, 7 May 2025 12:35:07 -0300 Subject: [PATCH 4/8] Implement string reversal without slice notation or reverse() --- src/string_reversal.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/string_reversal.py b/src/string_reversal.py index 1dfb1bff..fbefab5b 100644 --- a/src/string_reversal.py +++ b/src/string_reversal.py @@ -1,6 +1,6 @@ def reverse_string(input_string: str) -> str: """ - Reverse the given input string. + Reverse the given input string manually. Args: input_string (str): The string to be reversed. @@ -15,5 +15,9 @@ def reverse_string(input_string: str) -> str: 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 + # Manual string reversal using list conversion and iteration + reversed_chars = [] + for char in input_string: + reversed_chars.insert(0, char) + + return ''.join(reversed_chars) \ No newline at end of file From 3b5f4cad5fe7d3a44f214c173a205585c5bfd512 Mon Sep 17 00:00:00 2001 From: laura-abro Date: Wed, 7 May 2025 12:36:13 -0300 Subject: [PATCH 5/8] Start draft PR From b67f0357db3756d641523c86d1220399bbd6556c Mon Sep 17 00:00:00 2001 From: laura-abro Date: Wed, 7 May 2025 12:36:35 -0300 Subject: [PATCH 6/8] Implement recursive array flattening function --- src/array_utils.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 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 00000000..a0906b75 --- /dev/null +++ b/src/array_utils.py @@ -0,0 +1,36 @@ +from typing import List, Union, Any + +def flatten_array(arr: Union[List[Any], Any]) -> List[Any]: + """ + Recursively flatten a nested array of arbitrary depth into a single-level list. + + Args: + arr (Union[List[Any], Any]): The input array to be flattened. + Can be a nested list or a single element. + + Returns: + List[Any]: A flattened list containing all elements from the input. + + Examples: + >>> flatten_array([1, [2, 3], [4, [5, 6]]]) + [1, 2, 3, 4, 5, 6] + >>> flatten_array([]) + [] + >>> flatten_array(1) + [1] + """ + # Base case: if input is not a list, return it as a single-element list + if not isinstance(arr, list): + return [arr] + + # If list is empty, return empty list + if not arr: + return [] + + # Recursive flattening + 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 804b1c2a43864d515f94f718508ad522ee1a499b Mon Sep 17 00:00:00 2001 From: laura-abro Date: Wed, 7 May 2025 12:36:43 -0300 Subject: [PATCH 7/8] Add comprehensive tests for array flattening function --- tests/test_array_utils.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 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 00000000..96f268e4 --- /dev/null +++ b/tests/test_array_utils.py @@ -0,0 +1,31 @@ +import pytest +from src.array_utils import flatten_array + +def test_flatten_simple_nested_array(): + """Test flattening a simply nested array.""" + assert flatten_array([1, [2, 3], 4]) == [1, 2, 3, 4] + +def test_flatten_deeply_nested_array(): + """Test flattening a deeply nested array.""" + assert flatten_array([1, [2, [3, 4]], [5, [6, 7]]]) == [1, 2, 3, 4, 5, 6, 7] + +def test_flatten_empty_array(): + """Test flattening an empty array.""" + assert flatten_array([]) == [] + +def test_flatten_single_element(): + """Test flattening a single non-list element.""" + assert flatten_array(5) == [5] + +def test_flatten_mixed_types_array(): + """Test flattening an array with mixed types and nesting.""" + assert flatten_array([1, 'a', [2, [3.14]], [True, []]]) == [1, 'a', 2, 3.14, True] + +def test_flatten_nested_empty_arrays(): + """Test flattening an array with nested empty arrays.""" + assert flatten_array([1, [], [2, []], 3]) == [1, 2, 3] + +def test_flatten_multiple_level_nesting(): + """Test flattening an array with multiple levels of nesting.""" + nested_array = [1, [2, [3, [4, [5]]]], 6] + assert flatten_array(nested_array) == [1, 2, 3, 4, 5, 6] \ No newline at end of file From 9ffd8bc6e0f1ca4287869b5a610a50d5be798dba Mon Sep 17 00:00:00 2001 From: momstrosity Date: Wed, 7 May 2025 12:38:35 -0300 Subject: [PATCH 8/8] Start draft PR