File tree Expand file tree Collapse file tree 2 files changed +28
-10
lines changed
workspaces/adventure-pack/src/app/__tests__/__snapshots__ Expand file tree Collapse file tree 2 files changed +28
-10
lines changed Original file line number Diff line number Diff line change @@ -2835,17 +2835,26 @@ exports[`App can equip single goody: Python 3 flatten 1`] = `
2835
2835
# Running at: https://example.com/
2836
2836
2837
2837
from typing import Generator, TypeVar
2838
+ from math import inf
2838
2839
2839
2840
T = TypeVar("T")
2840
2841
NestedList = T | list["NestedList"]
2841
2842
2842
2843
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)
2847
2856
else:
2848
- yield i
2857
+ yield item
2849
2858
2850
2859
########################### END ADVENTURE PACK CODE ############################"
2851
2860
`;
Original file line number Diff line number Diff line change @@ -1579,17 +1579,26 @@ exports[`App can render goody: Python 3 UnionFind 1`] = `
1579
1579
1580
1580
exports[`App can render goody: Python 3 flatten 1`] = `
1581
1581
"from typing import Generator, TypeVar
1582
+ from math import inf
1582
1583
1583
1584
T = TypeVar("T")
1584
1585
NestedList = T | list["NestedList"]
1585
1586
1586
1587
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)
1591
1600
else:
1592
- yield i "
1601
+ yield item "
1593
1602
`;
1594
1603
1595
1604
exports[`App can render goody: Python 3 int.digits 1`] = `
You can’t perform that action at this time.
0 commit comments