Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import Generator, TypeVar

T = TypeVar("T")

nested_list = T | list["nested_list"]


def flatten(self: nested_list) -> Generator[T, None, None]:
for i in self:
if isinstance(i, list):
yield from flatten(i)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I like it.

One thing to consider: this flattens one level of the input however I believe in many libraries flatten recursively flattens nested lists as well, or at least allow you to configure how many levels of nesting to flatten. I'm cool with starting with this, but it would be nice to allow for deeper flattening in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this code does recursively flatten nested lists to a single layer.
Would we still want to configure how many layers it flattens?

Copy link
Contributor

@miorel miorel Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, I missed that for some reason. Yeah, I think it might be nice to configure. JavaScript supports this (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) as does Ruby (https://apidock.com/ruby/Array/flatten).

But let's do that in a follow-up PR since this one is already working well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created #387.

else:
yield i
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "flatten"
}
17 changes: 17 additions & 0 deletions workspaces/adventure-pack/goodies/python3/src/flatten/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from . import *


def test_flatten():
input_ = [[3, 1, 4, 1], [[5, 9, [2, 6]], [5, 3, 5]], 8, 9]
expected = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]
flattened = list(flatten(input_))
assert expected == flattened


def test_flatten_random_types():
dictionary = {"something": "something_else"}
l = [["hi", -80, "c", 0.0], [[(3, 3), dictionary, 6], [5, 4, 3]], 2, 1, 0]
flattened = list(flatten(l))
assert isinstance(l[0], list)
expected = ["hi", -80, "c", 0.0, (3, 3), dictionary, 6, 5, 4, 3, 2, 1, 0]
assert flattened == expected
Original file line number Diff line number Diff line change
Expand Up @@ -2535,6 +2535,28 @@ class UnionFind:
########################### END ADVENTURE PACK CODE ############################"
`;

exports[`App can equip single goody: Python 3 flatten 1`] = `
"########################## BEGIN ADVENTURE PACK CODE ###########################
# Adventure Pack commit fake-commit-hash
# Running at: https://example.com/

from typing import Generator, TypeVar

T = TypeVar("T")

nested_list = T | list["nested_list"]


def flatten(self: nested_list) -> Generator[T, None, None]:
for i in self:
if isinstance(i, list):
yield from flatten(i)
else:
yield i

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

exports[`App can equip single goody: Python 3 int.digits 1`] = `
"########################## BEGIN ADVENTURE PACK CODE ###########################
# Adventure Pack commit fake-commit-hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,22 @@ exports[`App can render goody: Python 3 UnionFind 1`] = `
return self.find(c1) == self.find(c2)"
`;

exports[`App can render goody: Python 3 flatten 1`] = `
"from typing import Generator, TypeVar

T = TypeVar("T")

nested_list = T | list["nested_list"]


def flatten(self: nested_list) -> Generator[T, None, None]:
for i in self:
if isinstance(i, list):
yield from flatten(i)
else:
yield i"
`;

exports[`App can render goody: Python 3 int.digits 1`] = `
"from typing import Generator

Expand Down