From 4ee2fdf0eeaa3b173d8864b372c9c798fa7ce71d Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Mon, 5 May 2025 13:11:21 -0300 Subject: [PATCH 1/9] Add string reversal function implementation --- 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..6c5177aa --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,19 @@ +def reverse_string(s: str) -> str: + """ + Reverse the given string. + + Args: + s (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(s, str): + raise TypeError("Input must be a string") + + # Return the reversed string + return s[::-1] \ No newline at end of file From 619f9afc5c9fea5f582dade2ff500783d8b1cb55 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Mon, 5 May 2025 13:11:29 -0300 Subject: [PATCH 2/9] Add comprehensive tests for string reversal function --- tests/test_string_reversal.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 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..aef7d8c4 --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,34 @@ +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_single_char(): + """Test reversing a single character string.""" + assert reverse_string("a") == "a" + +def test_reverse_string_with_spaces(): + """Test reversing a string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_string_with_special_chars(): + """Test reversing a string with special characters.""" + assert reverse_string("a1b2c3") == "3c2b1a" + +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) + + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(["hello"]) \ No newline at end of file From 0404f550590344feab3817b8edf0bedfc2d03520 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Mon, 5 May 2025 13:12:11 -0300 Subject: [PATCH 3/9] Implement manual string reversal without built-in methods --- src/string_reversal.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/string_reversal.py b/src/string_reversal.py index 6c5177aa..14ac38e0 100644 --- a/src/string_reversal.py +++ b/src/string_reversal.py @@ -1,6 +1,6 @@ def reverse_string(s: str) -> str: """ - Reverse the given string. + Reverse the given string manually, preserving all original characters. Args: s (str): The input string to be reversed. @@ -11,9 +11,23 @@ def reverse_string(s: str) -> str: Raises: TypeError: If the input is not a string. """ - # Check if input is a string + # Validate input is a string if not isinstance(s, str): raise TypeError("Input must be a string") - # Return the reversed string - return s[::-1] \ No newline at end of file + # Manual string reversal using a list and two-pointer technique + # Convert string to list to allow manipulation + chars = list(s) + + # Two-pointer approach to swap characters + left, right = 0, len(chars) - 1 + while left < right: + # Swap characters + chars[left], chars[right] = chars[right], chars[left] + + # Move pointers + left += 1 + right -= 1 + + # Convert back to string and return + return ''.join(chars) \ No newline at end of file From 31a6379805442f79029e147538a57c4e862a1b2a Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Mon, 5 May 2025 13:12:19 -0300 Subject: [PATCH 4/9] Update tests for comprehensive string reversal validation --- tests/test_string_reversal.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py index aef7d8c4..996358c7 100644 --- a/tests/test_string_reversal.py +++ b/tests/test_string_reversal.py @@ -19,8 +19,12 @@ def test_reverse_string_with_spaces(): assert reverse_string("hello world") == "dlrow olleh" def test_reverse_string_with_special_chars(): - """Test reversing a string with special characters.""" - assert reverse_string("a1b2c3") == "3c2b1a" + """Test reversing a string with special characters and mixed content.""" + assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" + +def test_reverse_string_with_unicode(): + """Test reversing a string with unicode characters.""" + assert reverse_string("café") == "éfac" def test_reverse_string_invalid_input(): """Test that TypeError is raised for non-string inputs.""" @@ -31,4 +35,4 @@ def test_reverse_string_invalid_input(): reverse_string(None) with pytest.raises(TypeError, match="Input must be a string"): - reverse_string(["hello"]) \ No newline at end of file + reverse_string(["hello"])) \ No newline at end of file From 007d5b572127fe29d0be633ca428851d1fca3031 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Mon, 5 May 2025 13:12:31 -0300 Subject: [PATCH 5/9] Fix syntax error in test file --- tests/test_string_reversal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py index 996358c7..94834877 100644 --- a/tests/test_string_reversal.py +++ b/tests/test_string_reversal.py @@ -35,4 +35,4 @@ def test_reverse_string_invalid_input(): reverse_string(None) with pytest.raises(TypeError, match="Input must be a string"): - reverse_string(["hello"])) \ No newline at end of file + reverse_string(["hello"]) \ No newline at end of file From 638c87d7c35ebdde6905365c8f7d9932fb0a7e9a Mon Sep 17 00:00:00 2001 From: laura-abro Date: Mon, 5 May 2025 13:13:32 -0300 Subject: [PATCH 6/9] Implement array flattening function with comprehensive documentation --- src/array_flattener.py | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/array_flattener.py diff --git a/src/array_flattener.py b/src/array_flattener.py new file mode 100644 index 00000000..5bba70d3 --- /dev/null +++ b/src/array_flattener.py @@ -0,0 +1,44 @@ +from typing import List, Union, Any + +def flatten_array(arr: List[Any]) -> List[Any]: + """ + Flatten a nested array (list) to a single-level list. + + This function recursively flattens a nested list of arbitrary depth + into a single-level list. It handles nested lists, tuples, and other + iterable types while preserving non-iterable elements. + + Args: + arr (List[Any]): The input nested list to be flattened. + + Returns: + List[Any]: A flattened list containing all non-list elements. + + Raises: + TypeError: If the input is not a list or iterable. + + 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([]) + [] + """ + # Check if input is a valid iterable + if not isinstance(arr, list): + raise TypeError("Input must be a list") + + # Initialize result list + flattened = [] + + # Iterate through each element + for item in arr: + # If item is a list or tuple, recursively flatten + if isinstance(item, (list, tuple)): + flattened.extend(flatten_array(item)) + else: + # Add non-list items directly + flattened.append(item) + + return flattened \ No newline at end of file From 5b543c98dffa7a9466f5a0f694f5cedcb8c5bb96 Mon Sep 17 00:00:00 2001 From: laura-abro Date: Mon, 5 May 2025 13:13:42 -0300 Subject: [PATCH 7/9] Add comprehensive tests for array flattening function --- tests/test_array_flattener.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 tests/test_array_flattener.py diff --git a/tests/test_array_flattener.py b/tests/test_array_flattener.py new file mode 100644 index 00000000..fbbf55de --- /dev/null +++ b/tests/test_array_flattener.py @@ -0,0 +1,33 @@ +import pytest +from src.array_flattener 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_empty_list(): + """Test flattening an empty list""" + assert flatten_array([]) == [] + +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_mixed_types(): + """Test flattening a list with mixed types""" + assert flatten_array([1, 'a', [2, 'b'], [3, [4, 'c']]]) == [1, 'a', 2, 'b', 3, 4, 'c'] + +def test_flatten_tuple_nested(): + """Test flattening a list with tuple nesting""" + assert flatten_array([1, (2, 3), [4, (5, 6)]]) == [1, 2, 3, 4, 5, 6] + +def test_invalid_input_type(): + """Test that TypeError is raised for non-list input""" + with pytest.raises(TypeError, match="Input must be a list"): + flatten_array("not a list") + with pytest.raises(TypeError, match="Input must be a list"): + flatten_array(123) \ No newline at end of file From 7a6bbf74254de03b8e887f91d074db265c4a22c7 Mon Sep 17 00:00:00 2001 From: laura-abro Date: Mon, 5 May 2025 13:13:57 -0300 Subject: [PATCH 8/9] Update array flattener to support tuples --- src/array_flattener.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/array_flattener.py b/src/array_flattener.py index 5bba70d3..7d65ea74 100644 --- a/src/array_flattener.py +++ b/src/array_flattener.py @@ -1,21 +1,21 @@ -from typing import List, Union, Any +from typing import List, Union, Any, Iterable -def flatten_array(arr: List[Any]) -> List[Any]: +def flatten_array(arr: Union[List[Any], Tuple[Any, ...]]) -> List[Any]: """ - Flatten a nested array (list) to a single-level list. + Flatten a nested array (list or tuple) to a single-level list. - This function recursively flattens a nested list of arbitrary depth + This function recursively flattens a nested list or tuple of arbitrary depth into a single-level list. It handles nested lists, tuples, and other iterable types while preserving non-iterable elements. Args: - arr (List[Any]): The input nested list to be flattened. + arr (Union[List[Any], Tuple[Any, ...]]): The input nested list/tuple to be flattened. Returns: - List[Any]: A flattened list containing all non-list elements. + List[Any]: A flattened list containing all non-list/non-tuple elements. Raises: - TypeError: If the input is not a list or iterable. + TypeError: If the input is not a list or tuple. Examples: >>> flatten_array([1, [2, 3], [4, [5, 6]]]) @@ -26,8 +26,8 @@ def flatten_array(arr: List[Any]) -> List[Any]: [] """ # Check if input is a valid iterable - if not isinstance(arr, list): - raise TypeError("Input must be a list") + if not isinstance(arr, (list, tuple)): + raise TypeError("Input must be a list or tuple") # Initialize result list flattened = [] @@ -38,7 +38,7 @@ def flatten_array(arr: List[Any]) -> List[Any]: if isinstance(item, (list, tuple)): flattened.extend(flatten_array(item)) else: - # Add non-list items directly + # Add non-list/non-tuple items directly flattened.append(item) return flattened \ No newline at end of file From 2dfc11df174789029c5fce3cc707f76aa72f7c57 Mon Sep 17 00:00:00 2001 From: laura-abro Date: Mon, 5 May 2025 13:14:05 -0300 Subject: [PATCH 9/9] Add missing Tuple import --- src/array_flattener.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/array_flattener.py b/src/array_flattener.py index 7d65ea74..f103f0e1 100644 --- a/src/array_flattener.py +++ b/src/array_flattener.py @@ -1,4 +1,4 @@ -from typing import List, Union, Any, Iterable +from typing import List, Union, Any, Iterable, Tuple def flatten_array(arr: Union[List[Any], Tuple[Any, ...]]) -> List[Any]: """