forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflatten.py
More file actions
61 lines (46 loc) · 1.68 KB
/
flatten.py
File metadata and controls
61 lines (46 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Flatten Arrays
Given an array that may contain nested arrays, produce a single
flat resultant array.
Reference: https://en.wikipedia.org/wiki/Flatten_(higher-order_function)
Complexity:
Time: O(n) where n is the total number of elements
Space: O(n)
"""
from __future__ import annotations
from collections.abc import Generator, Iterable
from typing import Any
def flatten(input_arr: Iterable[Any], output_arr: list[Any] | None = None) -> list[Any]:
"""Recursively flatten a nested iterable into a single list.
Args:
input_arr: A potentially nested iterable to flatten.
output_arr: Accumulator list for recursive calls (internal use).
Returns:
A flat list containing all leaf elements.
Examples:
>>> flatten([2, 1, [3, [4, 5], 6], 7, [8]])
[2, 1, 3, 4, 5, 6, 7, 8]
"""
if output_arr is None:
output_arr = []
for element in input_arr:
if not isinstance(element, str) and isinstance(element, Iterable):
flatten(element, output_arr)
else:
output_arr.append(element)
return output_arr
def flatten_iter(iterable: Iterable[Any]) -> Generator[Any, None, None]:
"""Lazily flatten a nested iterable, yielding one element at a time.
Args:
iterable: A potentially nested iterable to flatten.
Returns:
A generator producing one-dimensional output.
Examples:
>>> list(flatten_iter([2, 1, [3, [4, 5], 6], 7, [8]]))
[2, 1, 3, 4, 5, 6, 7, 8]
"""
for element in iterable:
if not isinstance(element, str) and isinstance(element, Iterable):
yield from flatten_iter(element)
else:
yield element