From 4532833de7d63d8c28038c1b15338162ada5ee1a Mon Sep 17 00:00:00 2001 From: laura-ct Date: Tue, 20 May 2025 17:41:05 +0000 Subject: [PATCH 1/3] Start draft PR From f810bb1bcfaf0e132c1bbb617cb36c7e79ca1a47 Mon Sep 17 00:00:00 2001 From: laura-ct Date: Tue, 20 May 2025 17:41:27 +0000 Subject: [PATCH 2/3] Add array flattening function with recursive implementation --- src/array_utils.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 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..6744086 --- /dev/null +++ b/src/array_utils.py @@ -0,0 +1,26 @@ +from typing import List, Union, Any + +def flatten_array(arr: Union[List[Any], Any]) -> List[Any]: + """ + Recursively flatten a nested list/array into a single-level list. + + Args: + arr (Union[List[Any], Any]): The input array to be flattened. + + Returns: + List[Any]: A flattened list containing all non-list elements. + + Raises: + TypeError: If the input is not a list or does not support iteration. + """ + # Base case: if input is not a list or is a string, return as single-element list + if not isinstance(arr, list) or isinstance(arr, str): + return [arr] + + # 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 d51e06a595bcbe075d4ef2992754b6c8ff005f7d Mon Sep 17 00:00:00 2001 From: laura-ct Date: Tue, 20 May 2025 17:41:36 +0000 Subject: [PATCH 3/3] Add comprehensive test cases for array flattening function --- tests/test_array_utils.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 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..b739e87 --- /dev/null +++ b/tests/test_array_utils.py @@ -0,0 +1,36 @@ +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]) == [1, 2, 3, 4] + +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_multiple_levels_of_nesting(): + """Test flattening multiple levels of nested lists""" + nested = [1, [2, [3, [4, 5]]], 6] + assert flatten_array(nested) == [1, 2, 3, 4, 5, 6] + +def test_flatten_mixed_types(): + """Test flattening a list with mixed types""" + mixed = [1, 'a', [2, [3.14]], {'key': 'value'}] + assert flatten_array(mixed) == [1, 'a', 2, 3.14, {'key': 'value'}] + +def test_flatten_empty_list(): + """Test flattening an empty list""" + assert flatten_array([]) == [] + +def test_flatten_non_nested_element(): + """Test flattening a non-nested element""" + assert flatten_array(42) == [42] + +def test_flatten_string(): + """Test that strings are treated as single elements""" + assert flatten_array('hello') == ['hello'] \ No newline at end of file