From fa333cfc70c116ce981dcda7dc047f26e77c95e5 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 18 Mar 2026 18:03:44 -0400 Subject: [PATCH] Add flatten-array --- config.json | 8 +++ exercises/practice/flatten-array/.busted | 5 ++ .../.docs/instructions.append.md | 9 +++ .../flatten-array/.docs/instructions.md | 16 +++++ .../flatten-array/.docs/introduction.md | 7 +++ .../practice/flatten-array/.meta/config.json | 19 ++++++ .../practice/flatten-array/.meta/example.moon | 15 +++++ .../flatten-array/.meta/spec_generator.moon | 34 ++++++++++ .../practice/flatten-array/.meta/tests.toml | 63 +++++++++++++++++++ .../practice/flatten-array/flatten_array.moon | 4 ++ .../flatten-array/flatten_array_spec.moon | 57 +++++++++++++++++ 11 files changed, 237 insertions(+) create mode 100644 exercises/practice/flatten-array/.busted create mode 100644 exercises/practice/flatten-array/.docs/instructions.append.md create mode 100644 exercises/practice/flatten-array/.docs/instructions.md create mode 100644 exercises/practice/flatten-array/.docs/introduction.md create mode 100644 exercises/practice/flatten-array/.meta/config.json create mode 100644 exercises/practice/flatten-array/.meta/example.moon create mode 100644 exercises/practice/flatten-array/.meta/spec_generator.moon create mode 100644 exercises/practice/flatten-array/.meta/tests.toml create mode 100644 exercises/practice/flatten-array/flatten_array.moon create mode 100644 exercises/practice/flatten-array/flatten_array_spec.moon diff --git a/config.json b/config.json index be183d8..36b61f5 100644 --- a/config.json +++ b/config.json @@ -394,6 +394,14 @@ "prerequisites": [], "difficulty": 4 }, + { + "slug": "flatten-array", + "name": "Flatten Array", + "uuid": "9f9b0efb-3bd0-4dc5-8df7-1edaf68ea046", + "practices": [], + "prerequisites": [], + "difficulty": 4 + }, { "slug": "isbn-verifier", "name": "ISBN Verifier", diff --git a/exercises/practice/flatten-array/.busted b/exercises/practice/flatten-array/.busted new file mode 100644 index 0000000..86b84e7 --- /dev/null +++ b/exercises/practice/flatten-array/.busted @@ -0,0 +1,5 @@ +return { + default = { + ROOT = { '.' } + } +} diff --git a/exercises/practice/flatten-array/.docs/instructions.append.md b/exercises/practice/flatten-array/.docs/instructions.append.md new file mode 100644 index 0000000..4c45e1b --- /dev/null +++ b/exercises/practice/flatten-array/.docs/instructions.append.md @@ -0,0 +1,9 @@ +# dummy + +## Moonscript-specific instructions + +Lua, and Moonscript, cannot store the value `nil` in a table. +Lua's concept of `nil` is the _absence_ of a value. +Lua uses the assignment of `nil` to a table key to delete that key from the table. + +In this exercise, you'll exclude the **string** `"null"` from the flattened array. diff --git a/exercises/practice/flatten-array/.docs/instructions.md b/exercises/practice/flatten-array/.docs/instructions.md new file mode 100644 index 0000000..b5b8271 --- /dev/null +++ b/exercises/practice/flatten-array/.docs/instructions.md @@ -0,0 +1,16 @@ +# Instructions + +Take a nested array of any depth and return a fully flattened array. + +Note that some language tracks may include null-like values in the input array, and the way these values are represented varies by track. +Such values should be excluded from the flattened array. + +Additionally, the input may be of a different data type and contain different types, depending on the track. + +Check the test suite for details. + +## Example + +input: `[1, [2, 6, null], [[null, [4]], 5]]` + +output: `[1, 2, 6, 4, 5]` diff --git a/exercises/practice/flatten-array/.docs/introduction.md b/exercises/practice/flatten-array/.docs/introduction.md new file mode 100644 index 0000000..a314857 --- /dev/null +++ b/exercises/practice/flatten-array/.docs/introduction.md @@ -0,0 +1,7 @@ +# Introduction + +A shipment of emergency supplies has arrived, but there's a problem. +To protect from damage, the items — flashlights, first-aid kits, blankets — are packed inside boxes, and some of those boxes are nested several layers deep inside other boxes! + +To be prepared for an emergency, everything must be easily accessible in one box. +Can you unpack all the supplies and place them into a single box, so they're ready when needed most? diff --git a/exercises/practice/flatten-array/.meta/config.json b/exercises/practice/flatten-array/.meta/config.json new file mode 100644 index 0000000..274f470 --- /dev/null +++ b/exercises/practice/flatten-array/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "glennj" + ], + "files": { + "solution": [ + "flatten_array.moon" + ], + "test": [ + "flatten_array_spec.moon" + ], + "example": [ + ".meta/example.moon" + ] + }, + "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.moon b/exercises/practice/flatten-array/.meta/example.moon new file mode 100644 index 0000000..3925045 --- /dev/null +++ b/exercises/practice/flatten-array/.meta/example.moon @@ -0,0 +1,15 @@ +{ + flatten: (input) -> + result = {} + + -- a recursive closure + _flatten = (list) -> + for elem in *list + if type(elem) == 'table' + _flatten elem + elseif elem != 'null' + table.insert result, elem + + _flatten input + result +} diff --git a/exercises/practice/flatten-array/.meta/spec_generator.moon b/exercises/practice/flatten-array/.meta/spec_generator.moon new file mode 100644 index 0000000..ba9ff78 --- /dev/null +++ b/exercises/practice/flatten-array/.meta/spec_generator.moon @@ -0,0 +1,34 @@ +json = (require 'dkjson').use_lpeg! + +int_list = (list) -> "{#{table.concat list, ', '}}" + +-- Lua can't store the literal `nil` in a table. nil, being the +-- absence of a value, is used to delete a key in a table. And +-- `ipairs` stops at the first index gap. Seems to me, the +-- easiest way to render an arbitrary list of lists is to +-- stringify it back into JSON, and then munge the string. +-- Unfortunately, we seem to lose _trailing_ nulls this way. + +nested_lists = (list, desc) -> + a = json.encode list + a = a\gsub '%[', '{' + a = a\gsub '%]', '}' + a = a\gsub ',', ', ' + a = a\gsub 'null', '"null"' + + if desc == "null values are omitted from the final result" + a\gsub '}$', ', "null"}' + else + a + +{ + module_imports: {'flatten'}, + + generate_test: (case, level) -> + lines = { + "input = #{nested_lists case.input.array, case.description}", + "expected = #{int_list case.expected}", + "assert.are.same expected, flatten input" + } + table.concat [indent line, level for line in *lines], '\n' +} diff --git a/exercises/practice/flatten-array/.meta/tests.toml b/exercises/practice/flatten-array/.meta/tests.toml new file mode 100644 index 0000000..44acf17 --- /dev/null +++ b/exercises/practice/flatten-array/.meta/tests.toml @@ -0,0 +1,63 @@ +# 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" +include = false + +[bc72da10-5f55-4ada-baf3-50e4da02ec8e] +description = "consecutive null values at the front of the array are omitted from the final result" +reimplements = "c6cf26de-8ccd-4410-84bd-b9efd88fd2bc" + +[382c5242-587e-4577-b8ce-a5fb51e385a1] +description = "consecutive null values in the middle of the list are omitted from the final result" +include = false + +[6991836d-0d9b-4703-80a0-3f1f23eb5981] +description = "consecutive null values in the middle of the array are omitted from the final result" +reimplements = "382c5242-587e-4577-b8ce-a5fb51e385a1" + +[ef1d4790-1b1e-4939-a179-51ace0829dbd] +description = "6 level nest list with null values" +include = false + +[dc90a09c-5376-449c-a7b3-c2d20d540069] +description = "6 level nested array with null values" +reimplements = "ef1d4790-1b1e-4939-a179-51ace0829dbd" + +[85721643-705a-4150-93ab-7ae398e2942d] +description = "all values in nested list are null" +include = false + +[51f5d9af-8f7f-4fb5-a156-69e8282cb275] +description = "all values in nested array are null" +reimplements = "85721643-705a-4150-93ab-7ae398e2942d" diff --git a/exercises/practice/flatten-array/flatten_array.moon b/exercises/practice/flatten-array/flatten_array.moon new file mode 100644 index 0000000..8e128f3 --- /dev/null +++ b/exercises/practice/flatten-array/flatten_array.moon @@ -0,0 +1,4 @@ +{ + flatten: (input) -> + error 'implement me' +} diff --git a/exercises/practice/flatten-array/flatten_array_spec.moon b/exercises/practice/flatten-array/flatten_array_spec.moon new file mode 100644 index 0000000..77aca82 --- /dev/null +++ b/exercises/practice/flatten-array/flatten_array_spec.moon @@ -0,0 +1,57 @@ +import flatten from require 'flatten_array' + +describe 'flatten-array', -> + it 'empty', -> + input = {} + expected = {} + assert.are.same expected, flatten input + + pending 'no nesting', -> + input = {0, 1, 2} + expected = {0, 1, 2} + assert.are.same expected, flatten input + + pending 'flattens a nested array', -> + input = {{{}}} + expected = {} + assert.are.same expected, flatten input + + pending 'flattens array with just integers present', -> + input = {1, {2, 3, 4, 5, 6, 7}, 8} + expected = {1, 2, 3, 4, 5, 6, 7, 8} + assert.are.same expected, flatten input + + pending '5 level nesting', -> + input = {0, 2, {{2, 3}, 8, 100, 4, {{{50}}}}, -2} + expected = {0, 2, 2, 3, 8, 100, 4, 50, -2} + assert.are.same expected, flatten input + + pending '6 level nesting', -> + input = {1, {2, {{3}}, {4, {{5}}}, 6, 7}, 8} + expected = {1, 2, 3, 4, 5, 6, 7, 8} + assert.are.same expected, flatten input + + pending 'null values are omitted from the final result', -> + input = {1, 2, "null"} + expected = {1, 2} + assert.are.same expected, flatten input + + pending 'consecutive null values at the front of the array are omitted from the final result', -> + input = {"null", "null", 3} + expected = {3} + assert.are.same expected, flatten input + + pending 'consecutive null values in the middle of the array are omitted from the final result', -> + input = {1, "null", "null", 4} + expected = {1, 4} + assert.are.same expected, flatten input + + pending '6 level nested array with null values', -> + input = {0, 2, {{2, 3}, 8, {{100}}, "null", {{}}}, -2} + expected = {0, 2, 2, 3, 8, 100, -2} + assert.are.same expected, flatten input + + pending 'all values in nested array are null', -> + input = {"null", {{{}}}, "null", "null", {{}}} + expected = {} + assert.are.same expected, flatten input