Skip to content

Commit 7aaf96f

Browse files
Sync flatten-array (#340)
Note that on this track we use lists instead of arrays for the flatten-array exercise. [no important files changed]
1 parent c4a902a commit 7aaf96f

4 files changed

Lines changed: 49 additions & 17 deletions

File tree

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# Instructions
22

3-
Take a nested list and return a single flattened list with all values except nil/null.
3+
Take a nested array of any depth and return a fully flattened array.
44

5-
The challenge is to take an arbitrarily-deep nested list-like structure and produce a flattened structure without any nil/null values.
5+
Note that some language tracks may include null-like values in the input array, and the way these values are represented varies by track.
6+
Such values should be excluded from the flattened array.
67

7-
For example:
8+
Additionally, the input may be of a different data type and contain different types, depending on the track.
89

9-
input: [1,[2,3,null,4],[null],5]
10+
Check the test suite for details.
1011

11-
output: [1,2,3,4,5]
12+
## Example
13+
14+
input: `[1, [2, 6, null], [[null, [4]], 5]]`
15+
16+
output: `[1, 2, 6, 4, 5]`
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Introduction
2+
3+
A shipment of emergency supplies has arrived, but there's a problem.
4+
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!
5+
6+
To be prepared for an emergency, everything must be easily accessible in one box.
7+
Can you unpack all the supplies and place them into a single box, so they're ready when needed most?

exercises/practice/flatten-array/.meta/tests.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,32 @@ description = "null values are omitted from the final result"
3232

3333
[c6cf26de-8ccd-4410-84bd-b9efd88fd2bc]
3434
description = "consecutive null values at the front of the list are omitted from the final result"
35+
include = false
36+
37+
[bc72da10-5f55-4ada-baf3-50e4da02ec8e]
38+
description = "consecutive null values at the front of the array are omitted from the final result"
39+
reimplements = "c6cf26de-8ccd-4410-84bd-b9efd88fd2bc"
3540

3641
[382c5242-587e-4577-b8ce-a5fb51e385a1]
3742
description = "consecutive null values in the middle of the list are omitted from the final result"
43+
include = false
44+
45+
[6991836d-0d9b-4703-80a0-3f1f23eb5981]
46+
description = "consecutive null values in the middle of the array are omitted from the final result"
47+
reimplements = "382c5242-587e-4577-b8ce-a5fb51e385a1"
3848

3949
[ef1d4790-1b1e-4939-a179-51ace0829dbd]
4050
description = "6 level nest list with null values"
51+
include = false
52+
53+
[dc90a09c-5376-449c-a7b3-c2d20d540069]
54+
description = "6 level nested array with null values"
55+
reimplements = "ef1d4790-1b1e-4939-a179-51ace0829dbd"
4156

4257
[85721643-705a-4150-93ab-7ae398e2942d]
4358
description = "all values in nested list are null"
59+
include = false
60+
61+
[51f5d9af-8f7f-4fb5-a156-69e8282cb275]
62+
description = "all values in nested array are null"
63+
reimplements = "85721643-705a-4150-93ab-7ae398e2942d"

exercises/practice/flatten-array/test.sml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ val testsuite =
2222
flatten nested |> Expect.equalTo [0, 1, 2]
2323
end),
2424

25-
test "flattens a nested array"
25+
test "flattens a nested list"
2626
(fn _ => let
2727
val nested = List [List [List []]]
2828
in
2929
flatten nested |> Expect.equalTo []
3030
end),
3131

32-
test "flattens array with just integers present"
32+
test "flattens list with just integers present"
3333
(fn _ => let
3434
val nested = List [Elem 1,
3535
List [Elem 2, Elem 3, Elem 4, Elem 5, Elem 6, Elem 7],
@@ -86,7 +86,7 @@ val testsuite =
8686
flatten nested |> Expect.equalTo [1, 4]
8787
end),
8888

89-
test "6 level nest list with Empty values"
89+
test "6 level nested list with Empty values"
9090
(fn _ => let
9191
val nested = List [Elem 0,
9292
Elem 2,
@@ -102,15 +102,15 @@ val testsuite =
102102

103103
test "all values in nested list are Empty"
104104
(fn _ => let
105-
val nested = List [Empty,
106-
List [List [List [Empty]]],
107-
Empty,
108-
Empty,
109-
List [List [Empty, Empty], Empty],
110-
Empty]
111-
in
112-
flatten nested |> Expect.equalTo []
113-
end)
105+
val nested = List [Empty,
106+
List [List [List [Empty]]],
107+
Empty,
108+
Empty,
109+
List [List [Empty, Empty], Empty],
110+
Empty]
111+
in
112+
flatten nested |> Expect.equalTo []
113+
end)
114114
]
115115

116116
val _ = Test.run testsuite

0 commit comments

Comments
 (0)