Skip to content

Commit 39d859d

Browse files
authored
Add flatten-array exercise (#189)
1 parent 170a0e1 commit 39d859d

File tree

7 files changed

+163
-0
lines changed

7 files changed

+163
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@
9898
"transforming"
9999
]
100100
},
101+
{
102+
"slug": "flatten-array",
103+
"name": "Flatten Array",
104+
"uuid": "091cf652-212f-44a6-8187-1a3acdb922e3",
105+
"practices": [],
106+
"prerequisites": [],
107+
"difficulty": 2
108+
},
101109
{
102110
"slug": "hello-world",
103111
"name": "Hello World",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Instructions
2+
3+
Take a nested list and return a single flattened list with all values except nil/null.
4+
5+
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.
6+
7+
For example:
8+
9+
input: [1,[2,3,null,4],[null],5]
10+
11+
output: [1,2,3,4,5]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"flatten_array.vim"
8+
],
9+
"test": [
10+
"flatten_array.vader"
11+
],
12+
"example": [
13+
".meta/example.vim"
14+
]
15+
},
16+
"blurb": "Take a nested list and return a single list with all values except nil/null.",
17+
"source": "Interview Question",
18+
"source_url": "https://reference.wolfram.com/language/ref/Flatten.html"
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function! Flatten(array) abort
2+
let l:results = []
3+
for l:elem in a:array
4+
if type(l:elem) == v:t_list
5+
call extend(l:results, Flatten(l:elem))
6+
elseif l:elem isnot v:null
7+
call add(l:results, l:elem)
8+
endif
9+
endfor
10+
11+
return results
12+
endfunction
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[8c71dabd-da60-422d-a290-4a571471fb14]
13+
description = "empty"
14+
15+
[d268b919-963c-442d-9f07-82b93f1b518c]
16+
description = "no nesting"
17+
18+
[3f15bede-c856-479e-bb71-1684b20c6a30]
19+
description = "flattens a nested array"
20+
21+
[c84440cc-bb3a-48a6-862c-94cf23f2815d]
22+
description = "flattens array with just integers present"
23+
24+
[d3d99d39-6be5-44f5-a31d-6037d92ba34f]
25+
description = "5 level nesting"
26+
27+
[d572bdba-c127-43ed-bdcd-6222ac83d9f7]
28+
description = "6 level nesting"
29+
30+
[0705a8e5-dc86-4cec-8909-150c5e54fa9c]
31+
description = "null values are omitted from the final result"
32+
33+
[c6cf26de-8ccd-4410-84bd-b9efd88fd2bc]
34+
description = "consecutive null values at the front of the list are omitted from the final result"
35+
36+
[382c5242-587e-4577-b8ce-a5fb51e385a1]
37+
description = "consecutive null values in the middle of the list are omitted from the final result"
38+
39+
[ef1d4790-1b1e-4939-a179-51ace0829dbd]
40+
description = "6 level nest list with null values"
41+
42+
[85721643-705a-4150-93ab-7ae398e2942d]
43+
description = "all values in nested list are null"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
Execute (empty):
3+
let g:array = []
4+
let g:expected = []
5+
AssertEqual g:expected, Flatten(g:array)
6+
7+
Execute (no nesting):
8+
let g:array = [0, 1, 2]
9+
let g:expected = [0, 1, 2]
10+
AssertEqual g:expected, Flatten(g:array)
11+
12+
Execute (flattens a nested array):
13+
let g:array = [[[]]]
14+
let g:expected = []
15+
AssertEqual g:expected, Flatten(g:array)
16+
17+
Execute (flattens array with just integers present):
18+
let g:array = [1, [2, 3, 4, 5, 6, 7], 8]
19+
let g:expected = [1, 2, 3, 4, 5, 6, 7, 8]
20+
AssertEqual g:expected, Flatten(g:array)
21+
22+
Execute (5 level nesting):
23+
let g:array = [0, 2, [[2, 3], 8, 100, 4, [[[50]]]], -2]
24+
let g:expected = [0, 2, 2, 3, 8, 100, 4, 50, -2]
25+
AssertEqual g:expected, Flatten(g:array)
26+
27+
Execute (6 level nesting):
28+
let g:array = [1, [2, [[3]], [4, [[5]]], 6, 7], 8]
29+
let g:expected = [1, 2, 3, 4, 5, 6, 7, 8]
30+
AssertEqual g:expected, Flatten(g:array)
31+
32+
Execute (null values are omitted from the final result):
33+
let g:array = [1, 2, v:null]
34+
let g:expected = [1, 2]
35+
AssertEqual g:expected, Flatten(g:array)
36+
37+
Execute (consecutive null values at the front of the list are omitted from the final result):
38+
let g:array = [v:null, v:null, 3]
39+
let g:expected = [3]
40+
AssertEqual g:expected, Flatten(g:array)
41+
42+
Execute (consecutive null values in the middle of the list are omitted from the final result):
43+
let g:array = [1, v:null, v:null, 4]
44+
let g:expected = [1, 4]
45+
AssertEqual g:expected, Flatten(g:array)
46+
47+
Execute (6 level nest list with null values):
48+
let g:array = [0, 2, [[2, 3], 8, [[100]], v:null, [[v:null]]], -2]
49+
let g:expected = [0, 2, 2, 3, 8, 100, -2]
50+
AssertEqual g:expected, Flatten(g:array)
51+
52+
Execute (all values in nested list are null):
53+
let g:array = [v:null, [[[v:null]]], v:null, v:null, [[v:null, v:null], v:null], v:null]
54+
let g:expected = []
55+
AssertEqual g:expected, Flatten(g:array)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"
2+
" Flattens an arbitrarily-deep nested array, removing v:null values.
3+
"
4+
" Examples:
5+
"
6+
" :echo Flatten([])
7+
" []
8+
" :echo Flatten([0, 1, 2])
9+
" [0, 1, 2]
10+
" :echo Flatten([0, 1, [2, 3, v:null]])
11+
" [0, 1, 2, 3]
12+
"
13+
function! Flatten(array) abort
14+
" your implemention goes here
15+
endfunction

0 commit comments

Comments
 (0)