Skip to content

Commit be9c977

Browse files
authored
Add flatten-array (#104)
* Add flatten-array * simplify * credit to Andras
1 parent e450b98 commit be9c977

File tree

12 files changed

+234
-1
lines changed

12 files changed

+234
-1
lines changed

bin/generate-spec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ canonical_data_path = "canonical-data/#{exercise_name}.json"
104104
assert os.execute('mkdir -p "$(dirname "' .. canonical_data_path .. '")"')
105105
assert os.execute('curl "' .. canonical_data_url .. '" -s -o "' .. canonical_data_path .. '"')
106106

107-
canonical_data = json.decode read_file canonical_data_path
107+
canonical_data = json.decode read_file(canonical_data_path), 1, json.null
108108

109109
tests_toml_path = exercise_directory .. '/.meta/tests.toml'
110110
included_tests = included_tests_from_toml tests_toml_path

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,14 @@
402402
"prerequisites": [],
403403
"difficulty": 4
404404
},
405+
{
406+
"slug": "flatten-array",
407+
"name": "Flatten Array",
408+
"uuid": "9f9b0efb-3bd0-4dc5-8df7-1edaf68ea046",
409+
"practices": [],
410+
"prerequisites": [],
411+
"difficulty": 4
412+
},
405413
{
406414
"slug": "isbn-verifier",
407415
"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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"contributors": [
6+
"BNAndras"
7+
],
8+
"files": {
9+
"solution": [
10+
"flatten_array.moon"
11+
],
12+
"test": [
13+
"flatten_array_spec.moon"
14+
],
15+
"example": [
16+
".meta/example.moon"
17+
]
18+
},
19+
"blurb": "Take a nested list and return a single list with all values except nil/null.",
20+
"source": "Interview Question",
21+
"source_url": "https://reference.wolfram.com/language/ref/Flatten.html"
22+
}
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
json = (require 'dkjson').use_lpeg!
2+
3+
int_list = (list) -> "{#{table.concat list, ', '}}"
4+
5+
-- A simple way to render an arbitrary list of lists is to stringify it
6+
-- and then munge the string into a Lua table literal.
7+
8+
nested_lists = (list) ->
9+
a = json.encode list
10+
a = a\gsub '%[', '{'
11+
a = a\gsub '%]', '}'
12+
a = a\gsub ',', ', '
13+
a = a\gsub 'null', '"null"'
14+
a
15+
16+
17+
{
18+
module_imports: {'flatten'},
19+
20+
generate_test: (case, level) ->
21+
lines = {
22+
"input = #{nested_lists case.input.array}",
23+
"expected = #{int_list case.expected}",
24+
"assert.are.same expected, flatten input"
25+
}
26+
table.concat [indent line, level for line in *lines], '\n'
27+
}
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"

0 commit comments

Comments
 (0)