From 6dca036bd4091c00eb6416f758e0efaeb4500a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=C2=A0Fertyk?= Date: Mon, 22 Jan 2024 20:31:53 +0100 Subject: [PATCH] Add flatten-array exercise (#39) --- config.json | 8 +++ .../flatten-array/.docs/instructions.md | 11 ++++ .../practice/flatten-array/.meta/config.json | 19 ++++++ .../practice/flatten-array/.meta/example.gd | 8 +++ .../practice/flatten-array/.meta/tests.toml | 43 +++++++++++++ .../practice/flatten-array/flatten_array.gd | 2 + .../flatten-array/flatten_array_test.gd | 64 +++++++++++++++++++ 7 files changed, 155 insertions(+) create mode 100644 exercises/practice/flatten-array/.docs/instructions.md create mode 100644 exercises/practice/flatten-array/.meta/config.json create mode 100644 exercises/practice/flatten-array/.meta/example.gd create mode 100644 exercises/practice/flatten-array/.meta/tests.toml create mode 100644 exercises/practice/flatten-array/flatten_array.gd create mode 100644 exercises/practice/flatten-array/flatten_array_test.gd diff --git a/config.json b/config.json index 2786f9c..a40f441 100644 --- a/config.json +++ b/config.json @@ -114,6 +114,14 @@ "practices": [], "prerequisites": [], "difficulty": 2 + }, + { + "slug": "flatten-array", + "name": "Flatten Array", + "uuid": "a9dd0e93-f34a-47dd-87d2-2e689f5f93f2", + "practices": [], + "prerequisites": [], + "difficulty": 2 } ] }, diff --git a/exercises/practice/flatten-array/.docs/instructions.md b/exercises/practice/flatten-array/.docs/instructions.md new file mode 100644 index 0000000..51bea67 --- /dev/null +++ b/exercises/practice/flatten-array/.docs/instructions.md @@ -0,0 +1,11 @@ +# Instructions + +Take a nested list and return a single flattened list with all values except nil/null. + +The challenge is to write a function that accepts an arbitrarily-deep nested list-like structure and returns a flattened structure without any nil/null values. + +For example: + +input: [1,[2,3,null,4],[null],5] + +output: [1,2,3,4,5] diff --git a/exercises/practice/flatten-array/.meta/config.json b/exercises/practice/flatten-array/.meta/config.json new file mode 100644 index 0000000..6d1695c --- /dev/null +++ b/exercises/practice/flatten-array/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "pfertyk" + ], + "files": { + "solution": [ + "flatten_array.gd" + ], + "test": [ + "flatten_array_test.gd" + ], + "example": [ + ".meta/example.gd" + ] + }, + "blurb": "Take a nested list and return a single list with all values except nil/null.", + "source": "Interview Question", + "source_url": "https://reference.wolfram.com/language/ref/Flatten.html" +} diff --git a/exercises/practice/flatten-array/.meta/example.gd b/exercises/practice/flatten-array/.meta/example.gd new file mode 100644 index 0000000..04c5643 --- /dev/null +++ b/exercises/practice/flatten-array/.meta/example.gd @@ -0,0 +1,8 @@ +func flatten(iterable): + var flatted = [] + for item in iterable: + if is_instance_of(item, TYPE_ARRAY): + flatted.append_array(flatten(item)) + elif item != null: + flatted.append(item) + return flatted diff --git a/exercises/practice/flatten-array/.meta/tests.toml b/exercises/practice/flatten-array/.meta/tests.toml new file mode 100644 index 0000000..6300219 --- /dev/null +++ b/exercises/practice/flatten-array/.meta/tests.toml @@ -0,0 +1,43 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[8c71dabd-da60-422d-a290-4a571471fb14] +description = "empty" + +[d268b919-963c-442d-9f07-82b93f1b518c] +description = "no nesting" + +[3f15bede-c856-479e-bb71-1684b20c6a30] +description = "flattens a nested array" + +[c84440cc-bb3a-48a6-862c-94cf23f2815d] +description = "flattens array with just integers present" + +[d3d99d39-6be5-44f5-a31d-6037d92ba34f] +description = "5 level nesting" + +[d572bdba-c127-43ed-bdcd-6222ac83d9f7] +description = "6 level nesting" + +[0705a8e5-dc86-4cec-8909-150c5e54fa9c] +description = "null values are omitted from the final result" + +[c6cf26de-8ccd-4410-84bd-b9efd88fd2bc] +description = "consecutive null values at the front of the list are omitted from the final result" + +[382c5242-587e-4577-b8ce-a5fb51e385a1] +description = "consecutive null values in the middle of the list are omitted from the final result" + +[ef1d4790-1b1e-4939-a179-51ace0829dbd] +description = "6 level nest list with null values" + +[85721643-705a-4150-93ab-7ae398e2942d] +description = "all values in nested list are null" diff --git a/exercises/practice/flatten-array/flatten_array.gd b/exercises/practice/flatten-array/flatten_array.gd new file mode 100644 index 0000000..40f0687 --- /dev/null +++ b/exercises/practice/flatten-array/flatten_array.gd @@ -0,0 +1,2 @@ +func flatten(iterable): + pass diff --git a/exercises/practice/flatten-array/flatten_array_test.gd b/exercises/practice/flatten-array/flatten_array_test.gd new file mode 100644 index 0000000..7949fed --- /dev/null +++ b/exercises/practice/flatten-array/flatten_array_test.gd @@ -0,0 +1,64 @@ +func test_empty(solution_script): + var inputs = [] + var expected = [] + return [solution_script.flatten(inputs), expected] + + +func test_no_nesting(solution_script): + var inputs = [0, 1, 2] + var expected = [0, 1, 2] + return [solution_script.flatten(inputs), expected] + + +func test_flattens_a_nested_array(solution_script): + var inputs = [[[]]] + var expected = [] + return [solution_script.flatten(inputs), expected] + + +func test_flattens_array_with_just_integers_present(solution_script): + var inputs = [1, [2, 3, 4, 5, 6, 7], 8] + var expected = [1, 2, 3, 4, 5, 6, 7, 8] + return [solution_script.flatten(inputs), expected] + + +func test_5_level_nesting(solution_script): + var inputs = [0, 2, [[2, 3], 8, 100, 4, [[[50]]]], -2] + var expected = [0, 2, 2, 3, 8, 100, 4, 50, -2] + return [solution_script.flatten(inputs), expected] + + +func test_6_level_nesting(solution_script): + var inputs = [1, [2, [[3]], [4, [[5]]], 6, 7], 8] + var expected = [1, 2, 3, 4, 5, 6, 7, 8] + return [solution_script.flatten(inputs), expected] + + +func test_null_values_are_omitted_from_the_final_result(solution_script): + var inputs = [1, 2, null] + var expected = [1, 2] + return [solution_script.flatten(inputs), expected] + + +func test_consecutive_null_values_at_the_front_of_the_list_are_omitted_from_the_final_result(solution_script): + var inputs = [null, null, 3] + var expected = [3] + return [solution_script.flatten(inputs), expected] + + +func test_consecutive_null_values_in_the_middle_of_the_list_are_omitted_from_the_final_result(solution_script): + var inputs = [1, null, null, 4] + var expected = [1, 4] + return [solution_script.flatten(inputs), expected] + + +func test_6_level_nest_list_with_null_values(solution_script): + var inputs = [0, 2, [[2, 3], 8, [[100]], null, [[null]]], -2] + var expected = [0, 2, 2, 3, 8, 100, -2] + return [solution_script.flatten(inputs), expected] + + +func test_all_values_in_nested_list_are_null(solution_script): + var inputs = [null, [[[null]]], null, null, [[null, null], null], null] + var expected = [] + return [solution_script.flatten(inputs), expected]