Skip to content

Commit ce5f213

Browse files
authored
Add depth parameter to the Python flatten function (#515)
Solves #387 .
1 parent f661d19 commit ce5f213

File tree

4 files changed

+77
-15
lines changed

4 files changed

+77
-15
lines changed
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
from typing import Generator, TypeVar
2+
from math import inf
23

34
T = TypeVar("T")
45
NestedList = T | list["NestedList"]
56

67

7-
def flatten(self: NestedList) -> Generator[T, None, None]:
8-
for i in self:
9-
if isinstance(i, list):
10-
yield from flatten(i)
8+
def flatten(
9+
nested_list: NestedList, depth: int = inf
10+
) -> Generator[T, None, None]:
11+
"""Flatten a nested list up to the specified depth.
12+
13+
Args:
14+
nested_list: The nested list to flatten
15+
depth: The maximum depth to flatten. Default is infinity (flatten all levels).
16+
"""
17+
for item in nested_list if isinstance(nested_list, list) else [nested_list]:
18+
if isinstance(item, list) and depth > 0:
19+
yield from flatten(item, depth - 1)
1120
else:
12-
yield i
21+
yield item

workspaces/adventure-pack/goodies/python3/src/flatten/test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,38 @@ def test_flatten_random_types():
1414
flattened = list(flatten(l))
1515
expected = ["hi", -80, "c", 0.0, (3, 3), dictionary, 6, 5, 4, 3, 2, 1, 0]
1616
assert flattened == expected
17+
18+
19+
def test_flatten_with_depth_0():
20+
# Test with depth=0 (no flattening)
21+
input_ = [1, [2, [3, [4, 5]]]]
22+
assert list(flatten(input_, depth=0)) == input_
23+
24+
25+
def test_flatten_with_depth_2():
26+
# Test with depth=2
27+
input_ = [1, [2, [3, [4, 5]]]]
28+
expected = [1, 2, 3, [4, 5]]
29+
assert list(flatten(input_, depth=2)) == expected
30+
31+
32+
def test_flatten_with_depth_greater_than_needed():
33+
# Test with depth larger than needed
34+
input_ = [1, [2, [3, [4, 5]]]]
35+
expected = [1, 2, 3, 4, 5]
36+
assert list(flatten(input_, depth=10)) == expected
37+
38+
39+
def test_flatten_with_empty_lists():
40+
# Test with empty lists at different levels
41+
input_ = [[], [1, [2, [], [3, []]], 4], []]
42+
assert list(flatten(input_, depth=1)) == [1, [2, [], [3, []]], 4]
43+
assert list(flatten(input_, depth=2)) == [1, 2, [], [3, []], 4]
44+
assert list(flatten(input_)) == [1, 2, 3, 4]
45+
46+
47+
def test_flatten_non_list_input():
48+
# Test with non-list input
49+
assert list(flatten(42)) == [42]
50+
assert list(flatten("hello")) == ["hello"]
51+
assert list(flatten(None)) == [None]

workspaces/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,17 +2835,26 @@ exports[`App can equip single goody: Python 3 flatten 1`] = `
28352835
# Running at: https://example.com/
28362836

28372837
from typing import Generator, TypeVar
2838+
from math import inf
28382839

28392840
T = TypeVar("T")
28402841
NestedList = T | list["NestedList"]
28412842

28422843

2843-
def flatten(self: NestedList) -> Generator[T, None, None]:
2844-
for i in self:
2845-
if isinstance(i, list):
2846-
yield from flatten(i)
2844+
def flatten(
2845+
nested_list: NestedList, depth: int = inf
2846+
) -> Generator[T, None, None]:
2847+
"""Flatten a nested list up to the specified depth.
2848+
2849+
Args:
2850+
nested_list: The nested list to flatten
2851+
depth: The maximum depth to flatten. Default is infinity (flatten all levels).
2852+
"""
2853+
for item in nested_list if isinstance(nested_list, list) else [nested_list]:
2854+
if isinstance(item, list) and depth > 0:
2855+
yield from flatten(item, depth - 1)
28472856
else:
2848-
yield i
2857+
yield item
28492858

28502859
########################### END ADVENTURE PACK CODE ############################"
28512860
`;

workspaces/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,17 +1579,26 @@ exports[`App can render goody: Python 3 UnionFind 1`] = `
15791579
15801580
exports[`App can render goody: Python 3 flatten 1`] = `
15811581
"from typing import Generator, TypeVar
1582+
from math import inf
15821583
15831584
T = TypeVar("T")
15841585
NestedList = T | list["NestedList"]
15851586
15861587
1587-
def flatten(self: NestedList) -> Generator[T, None, None]:
1588-
for i in self:
1589-
if isinstance(i, list):
1590-
yield from flatten(i)
1588+
def flatten(
1589+
nested_list: NestedList, depth: int = inf
1590+
) -> Generator[T, None, None]:
1591+
"""Flatten a nested list up to the specified depth.
1592+
1593+
Args:
1594+
nested_list: The nested list to flatten
1595+
depth: The maximum depth to flatten. Default is infinity (flatten all levels).
1596+
"""
1597+
for item in nested_list if isinstance(nested_list, list) else [nested_list]:
1598+
if isinstance(item, list) and depth > 0:
1599+
yield from flatten(item, depth - 1)
15911600
else:
1592-
yield i"
1601+
yield item"
15931602
`;
15941603
15951604
exports[`App can render goody: Python 3 int.digits 1`] = `

0 commit comments

Comments
 (0)