Skip to content

Commit fa333cf

Browse files
committed
Add flatten-array
1 parent 9639f1d commit fa333cf

File tree

11 files changed

+237
-0
lines changed

11 files changed

+237
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,14 @@
394394
"prerequisites": [],
395395
"difficulty": 4
396396
},
397+
{
398+
"slug": "flatten-array",
399+
"name": "Flatten Array",
400+
"uuid": "9f9b0efb-3bd0-4dc5-8df7-1edaf68ea046",
401+
"practices": [],
402+
"prerequisites": [],
403+
"difficulty": 4
404+
},
397405
{
398406
"slug": "isbn-verifier",
399407
"name": "ISBN Verifier",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# dummy
2+
3+
## Moonscript-specific instructions
4+
5+
Lua, and Moonscript, cannot store the value `nil` in a table.
6+
Lua's concept of `nil` is the _absence_ of a value.
7+
Lua uses the assignment of `nil` to a table key to delete that key from the table.
8+
9+
In this exercise, you'll exclude the **string** `"null"` from the flattened array.
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+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"flatten_array.moon"
8+
],
9+
"test": [
10+
"flatten_array_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
flatten: (input) ->
3+
result = {}
4+
5+
-- a recursive closure
6+
_flatten = (list) ->
7+
for elem in *list
8+
if type(elem) == 'table'
9+
_flatten elem
10+
elseif elem != 'null'
11+
table.insert result, elem
12+
13+
_flatten input
14+
result
15+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
json = (require 'dkjson').use_lpeg!
2+
3+
int_list = (list) -> "{#{table.concat list, ', '}}"
4+
5+
-- Lua can't store the literal `nil` in a table. nil, being the
6+
-- absence of a value, is used to delete a key in a table. And
7+
-- `ipairs` stops at the first index gap. Seems to me, the
8+
-- easiest way to render an arbitrary list of lists is to
9+
-- stringify it back into JSON, and then munge the string.
10+
-- Unfortunately, we seem to lose _trailing_ nulls this way.
11+
12+
nested_lists = (list, desc) ->
13+
a = json.encode list
14+
a = a\gsub '%[', '{'
15+
a = a\gsub '%]', '}'
16+
a = a\gsub ',', ', '
17+
a = a\gsub 'null', '"null"'
18+
19+
if desc == "null values are omitted from the final result"
20+
a\gsub '}$', ', "null"}'
21+
else
22+
a
23+
24+
{
25+
module_imports: {'flatten'},
26+
27+
generate_test: (case, level) ->
28+
lines = {
29+
"input = #{nested_lists case.input.array, case.description}",
30+
"expected = #{int_list case.expected}",
31+
"assert.are.same expected, flatten input"
32+
}
33+
table.concat [indent line, level for line in *lines], '\n'
34+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
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"
40+
41+
[382c5242-587e-4577-b8ce-a5fb51e385a1]
42+
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"
48+
49+
[ef1d4790-1b1e-4939-a179-51ace0829dbd]
50+
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"
56+
57+
[85721643-705a-4150-93ab-7ae398e2942d]
58+
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"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
flatten: (input) ->
3+
error 'implement me'
4+
}

0 commit comments

Comments
 (0)