Skip to content

Commit a11f66c

Browse files
authored
Add flatten-array (#358)
1 parent 0243656 commit a11f66c

File tree

7 files changed

+153
-0
lines changed

7 files changed

+153
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@
185185
"prerequisites": [],
186186
"difficulty": 2
187187
},
188+
{
189+
"slug": "flatten-array",
190+
"name": "Flatten Array",
191+
"uuid": "c738dc79-13ca-4a61-b25d-23fde54d588f",
192+
"practices": [],
193+
"prerequisites": [],
194+
"difficulty": 2
195+
},
188196
{
189197
"slug": "gigasecond",
190198
"name": "Gigasecond",
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 take an arbitrarily-deep nested list-like structure and produce 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.coffee"
8+
],
9+
"test": [
10+
"flatten-array.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class FlattenArray
2+
@flatten: (values) ->
3+
values.filter (value) -> value != null and value != undefined
4+
.reduce (flattened, value) ->
5+
if Array.isArray value
6+
flattened.concat FlattenArray.flatten value
7+
else
8+
flattened.concat value
9+
, []
10+
11+
module.exports = FlattenArray
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class FlattenArray
2+
@flatten: (values) ->
3+
4+
module.exports = FlattenArray
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
FlattenArray = require './flatten-array'
2+
3+
describe 'Flatten Array', ->
4+
it 'empty', ->
5+
values = []
6+
expected = []
7+
expect(FlattenArray.flatten values).toEqual expected
8+
9+
it 'no nesting', ->
10+
values = [0, 1, 2]
11+
expected = [0, 1, 2]
12+
expect(FlattenArray.flatten values).toEqual expected
13+
14+
it 'flattens array with just integers present', ->
15+
values = [1, [2, 3, 4, 5, 6, 7], 8]
16+
expected = [1, 2, 3, 4, 5, 6, 7, 8]
17+
expect(FlattenArray.flatten values).toEqual expected
18+
19+
it '5 level nesting', ->
20+
values = [0, 2, [[2, 3], 8, 100, 4, [[[50]]]], -2]
21+
expected = [0, 2, 2, 3, 8, 100, 4, 50, -2]
22+
expect(FlattenArray.flatten values).toEqual expected
23+
24+
it '6 level nesting', ->
25+
values = [1, [2, [[[3]]], [[4, [[[5]]]], 6, 7], 8]]
26+
expected = [1, 2, 3, 4, 5, 6, 7, 8]
27+
expect(FlattenArray.flatten values).toEqual expected
28+
29+
it 'null values are omitted from the final result', ->
30+
values = [1, 2, null]
31+
expected = [1, 2]
32+
expect(FlattenArray.flatten values).toEqual expected
33+
34+
it 'consecutive null values at the front of the list are omitted from the final result', ->
35+
values = [null, null, 3]
36+
expected = [3]
37+
expect(FlattenArray.flatten values).toEqual expected
38+
39+
it 'consecutive null values in the middle of the list are omitted from the final result', ->
40+
values = [1, null, null, 4]
41+
expected = [1, 4]
42+
expect(FlattenArray.flatten values).toEqual expected
43+
44+
it '6 level nest list with null values', ->
45+
values = [0, 2, [[2, 3], 8, [[100]], null, [[null]]], -2]
46+
expected = [0, 2, 2, 3, 8, 100, -2]
47+
expect(FlattenArray.flatten values).toEqual expected
48+
49+
it 'all values in nested list are null', ->
50+
values = [null, [[[null]]], null, null, [[null, null], null], null]
51+
expected = []
52+
expect(FlattenArray.flatten values).toEqual expected
53+
54+
it 'undefined values are omitted from the final result', ->
55+
values = [1, 2, undefined]
56+
expected = [1, 2]
57+
expect(FlattenArray.flatten values).toEqual expected

0 commit comments

Comments
 (0)