Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/array_flattener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import List, Union, Any

def flatten_array(arr: List[Union[int, List[Any]]]) -> List[int]:
"""
Flatten a nested array of integers into a single-level array.

This function recursively flattens nested lists of arbitrary depth
into a single list of integers.

Args:
arr (List[Union[int, List[Any]]]): A potentially nested list of integers

Returns:
List[int]: A flattened list of integers

Raises:
TypeError: If the input is not a list or contains non-integer elements

Examples:
>>> flatten_array([1, [2, 3], 4])
[1, 2, 3, 4]
>>> flatten_array([1, [2, [3, 4]], 5])
[1, 2, 3, 4, 5]
"""
# Input validation
if not isinstance(arr, list):
raise TypeError("Input must be a list")

# Flattening logic using recursive approach
flattened = []
for item in arr:
# If item is a list, recursively flatten it
if isinstance(item, list):
flattened.extend(flatten_array(item))
# If item is an integer, add to the list
elif isinstance(item, int):
flattened.append(item)
else:
# Raise error for non-integer, non-list elements
raise TypeError(f"Invalid element type: {type(item)}. Only integers and nested lists are allowed.")

return flattened
41 changes: 41 additions & 0 deletions tests/test_array_flattener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pytest
from src.array_flattener import flatten_array

def test_flatten_simple_array():
"""Test flattening a simple non-nested array"""
assert flatten_array([1, 2, 3]) == [1, 2, 3]

def test_flatten_single_nested_array():
"""Test flattening an array with a single level of nesting"""
assert flatten_array([1, [2, 3], 4]) == [1, 2, 3, 4]

def test_flatten_multiple_nested_array():
"""Test flattening an array with multiple levels of nesting"""
assert flatten_array([1, [2, [3, 4]], 5]) == [1, 2, 3, 4, 5]

def test_flatten_deeply_nested_array():
"""Test flattening a deeply nested array"""
assert flatten_array([1, [2, [3, [4, [5]]]], 6]) == [1, 2, 3, 4, 5, 6]

def test_flatten_empty_array():
"""Test flattening an empty array"""
assert flatten_array([]) == []

def test_flatten_array_with_empty_nested_lists():
"""Test flattening an array with empty nested lists"""
assert flatten_array([1, [], [2, []], 3]) == [1, 2, 3]

def test_invalid_input_type():
"""Test that a TypeError is raised for non-list input"""
with pytest.raises(TypeError, match="Input must be a list"):
flatten_array(123)

def test_invalid_element_type():
"""Test that a TypeError is raised for non-integer elements"""
with pytest.raises(TypeError, match="Invalid element type"):
flatten_array([1, 'a', 2])

def test_nested_invalid_element_type():
"""Test that a TypeError is raised for nested non-integer elements"""
with pytest.raises(TypeError, match="Invalid element type"):
flatten_array([1, [2, 'a'], 3])