From 21542febd35f0881f2b560b504180b9937bb68dd Mon Sep 17 00:00:00 2001 From: Volodymyr Semenov Date: Thu, 22 Aug 2024 14:43:31 -0400 Subject: [PATCH 1/3] Added flatten goodie --- .../goodies/python3/src/flatten/__init__.py | 9 +++++++++ .../goodies/python3/src/flatten/goody.json | 3 +++ .../goodies/python3/src/flatten/test.py | 17 +++++++++++++++++ .../__tests__/__snapshots__/equip-test.ts.snap | 18 ++++++++++++++++++ .../__snapshots__/render-test.ts.snap | 12 ++++++++++++ 5 files changed, 59 insertions(+) create mode 100644 workspaces/adventure-pack/goodies/python3/src/flatten/__init__.py create mode 100644 workspaces/adventure-pack/goodies/python3/src/flatten/goody.json create mode 100644 workspaces/adventure-pack/goodies/python3/src/flatten/test.py 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..a9ea7517 --- /dev/null +++ b/workspaces/adventure-pack/goodies/python3/src/flatten/__init__.py @@ -0,0 +1,9 @@ +from typing import Generator, Any + + +def flatten(self: list[Any]) -> Generator[Any, 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..1d489567 --- /dev/null +++ b/workspaces/adventure-pack/goodies/python3/src/flatten/test.py @@ -0,0 +1,17 @@ +from . import * + + +def test_flatten(): + l = [[0, 1, 2, 3], [[4, 5, 6], [7, 8, 9]], 10, 11, 12] + flattened = list(flatten(l)) + assert isinstance(l[0], list) + for i in range(len(flattened)): + assert i == flattened[i] + + +def test_flatten_not_sorted(): + l = [[12, 11, 10, 9], [[8, 7, 6], [5, 4, 3]], 2, 1, 0] + flattened = list(flatten(l)) + assert isinstance(l[0], list) + for i in range(len(flattened)): + assert flattened[i] == len(flattened) - 1 - i 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..309c31eb 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,24 @@ 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, Any + + +def flatten(self: list[Any]) -> Generator[Any, 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..7570fa15 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,18 @@ 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, Any + + +def flatten(self: list[Any]) -> Generator[Any, 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 From 58189941e2ddb952fc55228bccde34393e7ef813 Mon Sep 17 00:00:00 2001 From: Volodymyr Semenov Date: Thu, 22 Aug 2024 14:48:52 -0400 Subject: [PATCH 2/3] Added test for a list of various types --- .../adventure-pack/goodies/python3/src/flatten/test.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/workspaces/adventure-pack/goodies/python3/src/flatten/test.py b/workspaces/adventure-pack/goodies/python3/src/flatten/test.py index 1d489567..eeab14b9 100644 --- a/workspaces/adventure-pack/goodies/python3/src/flatten/test.py +++ b/workspaces/adventure-pack/goodies/python3/src/flatten/test.py @@ -9,9 +9,11 @@ def test_flatten(): assert i == flattened[i] -def test_flatten_not_sorted(): - l = [[12, 11, 10, 9], [[8, 7, 6], [5, 4, 3]], 2, 1, 0] +def test_flatten_random_types(): + dictionary = {"something": "something_e;se"} + 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] for i in range(len(flattened)): - assert flattened[i] == len(flattened) - 1 - i + assert flattened[i] == expected[i] From 9ce0db25263977323249f91a54ed3b5bc4179856 Mon Sep 17 00:00:00 2001 From: Volodymyr Semenov Date: Fri, 23 Aug 2024 15:17:10 -0400 Subject: [PATCH 3/3] Improved Type Hints and tests --- .../goodies/python3/src/flatten/__init__.py | 8 ++++++-- .../goodies/python3/src/flatten/test.py | 14 ++++++-------- .../app/__tests__/__snapshots__/equip-test.ts.snap | 8 ++++++-- .../__tests__/__snapshots__/render-test.ts.snap | 8 ++++++-- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/workspaces/adventure-pack/goodies/python3/src/flatten/__init__.py b/workspaces/adventure-pack/goodies/python3/src/flatten/__init__.py index a9ea7517..03c08731 100644 --- a/workspaces/adventure-pack/goodies/python3/src/flatten/__init__.py +++ b/workspaces/adventure-pack/goodies/python3/src/flatten/__init__.py @@ -1,7 +1,11 @@ -from typing import Generator, Any +from typing import Generator, TypeVar +T = TypeVar("T") -def flatten(self: list[Any]) -> Generator[Any, None, None]: +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) diff --git a/workspaces/adventure-pack/goodies/python3/src/flatten/test.py b/workspaces/adventure-pack/goodies/python3/src/flatten/test.py index eeab14b9..b8de3bb0 100644 --- a/workspaces/adventure-pack/goodies/python3/src/flatten/test.py +++ b/workspaces/adventure-pack/goodies/python3/src/flatten/test.py @@ -2,18 +2,16 @@ def test_flatten(): - l = [[0, 1, 2, 3], [[4, 5, 6], [7, 8, 9]], 10, 11, 12] - flattened = list(flatten(l)) - assert isinstance(l[0], list) - for i in range(len(flattened)): - assert i == flattened[i] + 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_e;se"} + 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] - for i in range(len(flattened)): - assert flattened[i] == expected[i] + 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 309c31eb..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 @@ -2540,10 +2540,14 @@ exports[`App can equip single goody: Python 3 flatten 1`] = ` # Adventure Pack commit fake-commit-hash # Running at: https://example.com/ -from typing import Generator, Any +from typing import Generator, TypeVar +T = TypeVar("T") -def flatten(self: list[Any]) -> Generator[Any, None, None]: +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) 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 7570fa15..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 @@ -1420,10 +1420,14 @@ exports[`App can render goody: Python 3 UnionFind 1`] = ` `; exports[`App can render goody: Python 3 flatten 1`] = ` -"from typing import Generator, Any +"from typing import Generator, TypeVar +T = TypeVar("T") -def flatten(self: list[Any]) -> Generator[Any, None, None]: +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)