From a11f66c357f9fa0257069a245c62df70cc52ee9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Sat, 12 Oct 2024 23:29:38 -0700 Subject: [PATCH] Add `flatten-array` (#358) --- config.json | 8 +++ .../flatten-array/.docs/instructions.md | 11 ++++ .../practice/flatten-array/.meta/config.json | 19 +++++++ .../flatten-array/.meta/example.coffee | 11 ++++ .../practice/flatten-array/.meta/tests.toml | 43 ++++++++++++++ .../flatten-array/flatten-array.coffee | 4 ++ .../flatten-array/flatten-array.spec.coffee | 57 +++++++++++++++++++ 7 files changed, 153 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.coffee create mode 100644 exercises/practice/flatten-array/.meta/tests.toml create mode 100644 exercises/practice/flatten-array/flatten-array.coffee create mode 100644 exercises/practice/flatten-array/flatten-array.spec.coffee diff --git a/config.json b/config.json index 8fac868..a958747 100644 --- a/config.json +++ b/config.json @@ -185,6 +185,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "flatten-array", + "name": "Flatten Array", + "uuid": "c738dc79-13ca-4a61-b25d-23fde54d588f", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "gigasecond", "name": "Gigasecond", diff --git a/exercises/practice/flatten-array/.docs/instructions.md b/exercises/practice/flatten-array/.docs/instructions.md new file mode 100644 index 0000000..89dacfa --- /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 take an arbitrarily-deep nested list-like structure and produce 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..ac0858f --- /dev/null +++ b/exercises/practice/flatten-array/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "flatten-array.coffee" + ], + "test": [ + "flatten-array.spec.coffee" + ], + "example": [ + ".meta/example.coffee" + ] + }, + "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.coffee b/exercises/practice/flatten-array/.meta/example.coffee new file mode 100644 index 0000000..d32e4b2 --- /dev/null +++ b/exercises/practice/flatten-array/.meta/example.coffee @@ -0,0 +1,11 @@ +class FlattenArray + @flatten: (values) -> + values.filter (value) -> value != null and value != undefined + .reduce (flattened, value) -> + if Array.isArray value + flattened.concat FlattenArray.flatten value + else + flattened.concat value + , [] + +module.exports = FlattenArray 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.coffee b/exercises/practice/flatten-array/flatten-array.coffee new file mode 100644 index 0000000..d144abc --- /dev/null +++ b/exercises/practice/flatten-array/flatten-array.coffee @@ -0,0 +1,4 @@ +class FlattenArray + @flatten: (values) -> + +module.exports = FlattenArray diff --git a/exercises/practice/flatten-array/flatten-array.spec.coffee b/exercises/practice/flatten-array/flatten-array.spec.coffee new file mode 100644 index 0000000..00d1ecb --- /dev/null +++ b/exercises/practice/flatten-array/flatten-array.spec.coffee @@ -0,0 +1,57 @@ +FlattenArray = require './flatten-array' + +describe 'Flatten Array', -> + it 'empty', -> + values = [] + expected = [] + expect(FlattenArray.flatten values).toEqual expected + + it 'no nesting', -> + values = [0, 1, 2] + expected = [0, 1, 2] + expect(FlattenArray.flatten values).toEqual expected + + it 'flattens array with just integers present', -> + values = [1, [2, 3, 4, 5, 6, 7], 8] + expected = [1, 2, 3, 4, 5, 6, 7, 8] + expect(FlattenArray.flatten values).toEqual expected + + it '5 level nesting', -> + values = [0, 2, [[2, 3], 8, 100, 4, [[[50]]]], -2] + expected = [0, 2, 2, 3, 8, 100, 4, 50, -2] + expect(FlattenArray.flatten values).toEqual expected + + it '6 level nesting', -> + values = [1, [2, [[[3]]], [[4, [[[5]]]], 6, 7], 8]] + expected = [1, 2, 3, 4, 5, 6, 7, 8] + expect(FlattenArray.flatten values).toEqual expected + + it 'null values are omitted from the final result', -> + values = [1, 2, null] + expected = [1, 2] + expect(FlattenArray.flatten values).toEqual expected + + it 'consecutive null values at the front of the list are omitted from the final result', -> + values = [null, null, 3] + expected = [3] + expect(FlattenArray.flatten values).toEqual expected + + it 'consecutive null values in the middle of the list are omitted from the final result', -> + values = [1, null, null, 4] + expected = [1, 4] + expect(FlattenArray.flatten values).toEqual expected + + it '6 level nest list with null values', -> + values = [0, 2, [[2, 3], 8, [[100]], null, [[null]]], -2] + expected = [0, 2, 2, 3, 8, 100, -2] + expect(FlattenArray.flatten values).toEqual expected + + it 'all values in nested list are null', -> + values = [null, [[[null]]], null, null, [[null, null], null], null] + expected = [] + expect(FlattenArray.flatten values).toEqual expected + + it 'undefined values are omitted from the final result', -> + values = [1, 2, undefined] + expected = [1, 2] + expect(FlattenArray.flatten values).toEqual expected