diff --git a/config.json b/config.json index b1ecaa6..947d6f3 100644 --- a/config.json +++ b/config.json @@ -98,6 +98,14 @@ "transforming" ] }, + { + "slug": "flatten-array", + "name": "Flatten Array", + "uuid": "091cf652-212f-44a6-8187-1a3acdb922e3", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "hello-world", "name": "Hello World", 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..7a09c0d --- /dev/null +++ b/exercises/practice/flatten-array/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "flatten_array.vim" + ], + "test": [ + "flatten_array.vader" + ], + "example": [ + ".meta/example.vim" + ] + }, + "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.vim b/exercises/practice/flatten-array/.meta/example.vim new file mode 100644 index 0000000..43b0d4c --- /dev/null +++ b/exercises/practice/flatten-array/.meta/example.vim @@ -0,0 +1,12 @@ +function! Flatten(array) abort + let l:results = [] + for l:elem in a:array + if type(l:elem) == v:t_list + call extend(l:results, Flatten(l:elem)) + elseif l:elem isnot v:null + call add(l:results, l:elem) + endif + endfor + + return results +endfunction 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.vader b/exercises/practice/flatten-array/flatten_array.vader new file mode 100644 index 0000000..8909dc3 --- /dev/null +++ b/exercises/practice/flatten-array/flatten_array.vader @@ -0,0 +1,55 @@ + +Execute (empty): + let g:array = [] + let g:expected = [] + AssertEqual g:expected, Flatten(g:array) + +Execute (no nesting): + let g:array = [0, 1, 2] + let g:expected = [0, 1, 2] + AssertEqual g:expected, Flatten(g:array) + +Execute (flattens a nested array): + let g:array = [[[]]] + let g:expected = [] + AssertEqual g:expected, Flatten(g:array) + +Execute (flattens array with just integers present): + let g:array = [1, [2, 3, 4, 5, 6, 7], 8] + let g:expected = [1, 2, 3, 4, 5, 6, 7, 8] + AssertEqual g:expected, Flatten(g:array) + +Execute (5 level nesting): + let g:array = [0, 2, [[2, 3], 8, 100, 4, [[[50]]]], -2] + let g:expected = [0, 2, 2, 3, 8, 100, 4, 50, -2] + AssertEqual g:expected, Flatten(g:array) + +Execute (6 level nesting): + let g:array = [1, [2, [[3]], [4, [[5]]], 6, 7], 8] + let g:expected = [1, 2, 3, 4, 5, 6, 7, 8] + AssertEqual g:expected, Flatten(g:array) + +Execute (null values are omitted from the final result): + let g:array = [1, 2, v:null] + let g:expected = [1, 2] + AssertEqual g:expected, Flatten(g:array) + +Execute (consecutive null values at the front of the list are omitted from the final result): + let g:array = [v:null, v:null, 3] + let g:expected = [3] + AssertEqual g:expected, Flatten(g:array) + +Execute (consecutive null values in the middle of the list are omitted from the final result): + let g:array = [1, v:null, v:null, 4] + let g:expected = [1, 4] + AssertEqual g:expected, Flatten(g:array) + +Execute (6 level nest list with null values): + let g:array = [0, 2, [[2, 3], 8, [[100]], v:null, [[v:null]]], -2] + let g:expected = [0, 2, 2, 3, 8, 100, -2] + AssertEqual g:expected, Flatten(g:array) + +Execute (all values in nested list are null): + let g:array = [v:null, [[[v:null]]], v:null, v:null, [[v:null, v:null], v:null], v:null] + let g:expected = [] + AssertEqual g:expected, Flatten(g:array) diff --git a/exercises/practice/flatten-array/flatten_array.vim b/exercises/practice/flatten-array/flatten_array.vim new file mode 100644 index 0000000..1a8b4f3 --- /dev/null +++ b/exercises/practice/flatten-array/flatten_array.vim @@ -0,0 +1,15 @@ +" +" Flattens an arbitrarily-deep nested array, removing v:null values. +" +" Examples: +" +" :echo Flatten([]) +" [] +" :echo Flatten([0, 1, 2]) +" [0, 1, 2] +" :echo Flatten([0, 1, [2, 3, v:null]]) +" [0, 1, 2, 3] +" +function! Flatten(array) abort + " your implemention goes here +endfunction