diff --git a/workspaces/adventure-pack/goodies/python3/src/flatten/__init__.py b/workspaces/adventure-pack/goodies/python3/src/flatten/__init__.py new file mode 100644 index 00000000..03c08731 --- /dev/null +++ b/workspaces/adventure-pack/goodies/python3/src/flatten/__init__.py @@ -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) + else: + yield i diff --git a/workspaces/adventure-pack/goodies/python3/src/flatten/goody.json b/workspaces/adventure-pack/goodies/python3/src/flatten/goody.json new file mode 100644 index 00000000..5f25ae1f --- /dev/null +++ b/workspaces/adventure-pack/goodies/python3/src/flatten/goody.json @@ -0,0 +1,3 @@ +{ + "name": "flatten" +} diff --git a/workspaces/adventure-pack/goodies/python3/src/flatten/test.py b/workspaces/adventure-pack/goodies/python3/src/flatten/test.py new file mode 100644 index 00000000..b8de3bb0 --- /dev/null +++ b/workspaces/adventure-pack/goodies/python3/src/flatten/test.py @@ -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 diff --git a/workspaces/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap b/workspaces/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap index 02100edc..0ad03d9a 100644 --- a/workspaces/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap +++ b/workspaces/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap @@ -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 diff --git a/workspaces/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap b/workspaces/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap index 88ab7c34..719a7227 100644 --- a/workspaces/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap +++ b/workspaces/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap @@ -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