Skip to content

Commit 83dab98

Browse files
Add flatten-array exercise (#274)
1 parent c097273 commit 83dab98

File tree

8 files changed

+476
-0
lines changed

8 files changed

+476
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,14 @@
495495
"prerequisites": [],
496496
"difficulty": 4
497497
},
498+
{
499+
"slug": "flatten-array",
500+
"name": "Flatten Array",
501+
"uuid": "89db9aa8-83bd-4727-8eda-61de6b568690",
502+
"practices": [],
503+
"prerequisites": [],
504+
"difficulty": 4
505+
},
498506
{
499507
"slug": "flower-field",
500508
"name": "Flower Field",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Instructions
2+
3+
Take a nested array of any depth and return a fully flattened array.
4+
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.
7+
8+
Additionally, the input may be of a different data type and contain different types, depending on the track.
9+
10+
Check the test suite for details.
11+
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?
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"flatten-array.v"
8+
],
9+
"test": [
10+
"run_test.v"
11+
],
12+
"example": [
13+
".meta/example.v"
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module main
2+
3+
type Box[T] = None | One[T] | Many[T]
4+
5+
struct None {}
6+
7+
struct One[T] {
8+
value T
9+
}
10+
11+
struct Many[T] {
12+
boxes []Box[T]
13+
}
14+
15+
fn flatten[T](box Box[T]) []T {
16+
mut dest := []T{}
17+
append(mut dest, box)
18+
return dest
19+
}
20+
21+
fn append[T](mut dest []T, box Box[T]) {
22+
match box {
23+
None {}
24+
One[T] {
25+
dest << box.value
26+
}
27+
Many[T] {
28+
for element in box.boxes {
29+
append(mut dest, element)
30+
}
31+
}
32+
}
33+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[8c71dabd-da60-422d-a290-4a571471fb14]
6+
description = "empty"
7+
8+
[d268b919-963c-442d-9f07-82b93f1b518c]
9+
description = "no nesting"
10+
11+
[3f15bede-c856-479e-bb71-1684b20c6a30]
12+
description = "flattens a nested array"
13+
14+
[c84440cc-bb3a-48a6-862c-94cf23f2815d]
15+
description = "flattens array with just integers present"
16+
17+
[d3d99d39-6be5-44f5-a31d-6037d92ba34f]
18+
description = "5 level nesting"
19+
20+
[d572bdba-c127-43ed-bdcd-6222ac83d9f7]
21+
description = "6 level nesting"
22+
23+
[0705a8e5-dc86-4cec-8909-150c5e54fa9c]
24+
description = "null values are omitted from the final result"
25+
26+
[c6cf26de-8ccd-4410-84bd-b9efd88fd2bc]
27+
description = "consecutive null values at the front of the list are omitted from the final result"
28+
include = false
29+
30+
[bc72da10-5f55-4ada-baf3-50e4da02ec8e]
31+
description = "consecutive null values at the front of the array are omitted from the final result"
32+
reimplements = "c6cf26de-8ccd-4410-84bd-b9efd88fd2bc"
33+
34+
[382c5242-587e-4577-b8ce-a5fb51e385a1]
35+
description = "consecutive null values in the middle of the list are omitted from the final result"
36+
include = false
37+
38+
[6991836d-0d9b-4703-80a0-3f1f23eb5981]
39+
description = "consecutive null values in the middle of the array are omitted from the final result"
40+
reimplements = "382c5242-587e-4577-b8ce-a5fb51e385a1"
41+
42+
[ef1d4790-1b1e-4939-a179-51ace0829dbd]
43+
description = "6 level nest list with null values"
44+
include = false
45+
46+
[dc90a09c-5376-449c-a7b3-c2d20d540069]
47+
description = "6 level nested array with null values"
48+
reimplements = "ef1d4790-1b1e-4939-a179-51ace0829dbd"
49+
50+
[85721643-705a-4150-93ab-7ae398e2942d]
51+
description = "all values in nested list are null"
52+
include = false
53+
54+
[51f5d9af-8f7f-4fb5-a156-69e8282cb275]
55+
description = "all values in nested array are null"
56+
reimplements = "85721643-705a-4150-93ab-7ae398e2942d"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module main
2+
3+
type Box[T] = None | One[T] | Many[T]
4+
5+
struct None {}
6+
7+
struct One[T] {
8+
value T
9+
}
10+
11+
struct Many[T] {
12+
boxes []Box[T]
13+
}
14+
15+
fn flatten[T](box Box[T]) []T {
16+
// Please implement the `flatten` function
17+
}

0 commit comments

Comments
 (0)