Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "flatten-array",
"name": "Flatten Array",
"uuid": "9f9b0efb-3bd0-4dc5-8df7-1edaf68ea046",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "isbn-verifier",
"name": "ISBN Verifier",
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/flatten-array/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
default = {
ROOT = { '.' }
}
}
9 changes: 9 additions & 0 deletions exercises/practice/flatten-array/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# dummy

## Moonscript-specific instructions

Lua, and Moonscript, cannot store the value `nil` in a table.
Lua's concept of `nil` is the _absence_ of a value.
Lua uses the assignment of `nil` to a table key to delete that key from the table.

In this exercise, you'll exclude the **string** `"null"` from the flattened array.
16 changes: 16 additions & 0 deletions exercises/practice/flatten-array/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Instructions

Take a nested array of any depth and return a fully flattened array.

Note that some language tracks may include null-like values in the input array, and the way these values are represented varies by track.
Such values should be excluded from the flattened array.

Additionally, the input may be of a different data type and contain different types, depending on the track.

Check the test suite for details.

## Example

input: `[1, [2, 6, null], [[null, [4]], 5]]`

output: `[1, 2, 6, 4, 5]`
7 changes: 7 additions & 0 deletions exercises/practice/flatten-array/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Introduction

A shipment of emergency supplies has arrived, but there's a problem.
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!

To be prepared for an emergency, everything must be easily accessible in one box.
Can you unpack all the supplies and place them into a single box, so they're ready when needed most?
19 changes: 19 additions & 0 deletions exercises/practice/flatten-array/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"glennj"
],
"files": {
"solution": [
"flatten_array.moon"
],
"test": [
"flatten_array_spec.moon"
],
"example": [
".meta/example.moon"
]
},
"blurb": "Take a nested list and return a single list with all values except nil/null.",
"source": "Interview Question",
"source_url": "https://reference.wolfram.com/language/ref/Flatten.html"
}
15 changes: 15 additions & 0 deletions exercises/practice/flatten-array/.meta/example.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
flatten: (input) ->
result = {}

-- a recursive closure
_flatten = (list) ->
for elem in *list
if type(elem) == 'table'
_flatten elem
elseif elem != 'null'
table.insert result, elem

_flatten input
result
}
34 changes: 34 additions & 0 deletions exercises/practice/flatten-array/.meta/spec_generator.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
json = (require 'dkjson').use_lpeg!

int_list = (list) -> "{#{table.concat list, ', '}}"

-- Lua can't store the literal `nil` in a table. nil, being the
-- absence of a value, is used to delete a key in a table. And
-- `ipairs` stops at the first index gap. Seems to me, the
-- easiest way to render an arbitrary list of lists is to
-- stringify it back into JSON, and then munge the string.
-- Unfortunately, we seem to lose _trailing_ nulls this way.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json.decode has three parameters per https://dkolf.de/dkjson-lua/documentation. The third one lets us specify a replacement value besides the default nil for nulls in the JSON. The page documents a json.null for setting an explicit null value. In bin/generate-spec, we use canonical_data = json.decode read_file canonical_data_path, but we could instead use canonical_data = json.decode (read_file canonical_data_path), 1, json.null. That'll keep the trailing nulls in place although we'd still need to represent them as "null" in the final tables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!


nested_lists = (list, desc) ->
a = json.encode list
a = a\gsub '%[', '{'
a = a\gsub '%]', '}'
a = a\gsub ',', ', '
a = a\gsub 'null', '"null"'

if desc == "null values are omitted from the final result"
a\gsub '}$', ', "null"}'
else
a

{
module_imports: {'flatten'},

generate_test: (case, level) ->
lines = {
"input = #{nested_lists case.input.array, case.description}",
"expected = #{int_list case.expected}",
"assert.are.same expected, flatten input"
}
table.concat [indent line, level for line in *lines], '\n'
}
63 changes: 63 additions & 0 deletions exercises/practice/flatten-array/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[8c71dabd-da60-422d-a290-4a571471fb14]
description = "empty"

[d268b919-963c-442d-9f07-82b93f1b518c]
description = "no nesting"

[3f15bede-c856-479e-bb71-1684b20c6a30]
description = "flattens a nested array"

[c84440cc-bb3a-48a6-862c-94cf23f2815d]
description = "flattens array with just integers present"

[d3d99d39-6be5-44f5-a31d-6037d92ba34f]
description = "5 level nesting"

[d572bdba-c127-43ed-bdcd-6222ac83d9f7]
description = "6 level nesting"

[0705a8e5-dc86-4cec-8909-150c5e54fa9c]
description = "null values are omitted from the final result"

[c6cf26de-8ccd-4410-84bd-b9efd88fd2bc]
description = "consecutive null values at the front of the list are omitted from the final result"
include = false

[bc72da10-5f55-4ada-baf3-50e4da02ec8e]
description = "consecutive null values at the front of the array are omitted from the final result"
reimplements = "c6cf26de-8ccd-4410-84bd-b9efd88fd2bc"

[382c5242-587e-4577-b8ce-a5fb51e385a1]
description = "consecutive null values in the middle of the list are omitted from the final result"
include = false

[6991836d-0d9b-4703-80a0-3f1f23eb5981]
description = "consecutive null values in the middle of the array are omitted from the final result"
reimplements = "382c5242-587e-4577-b8ce-a5fb51e385a1"

[ef1d4790-1b1e-4939-a179-51ace0829dbd]
description = "6 level nest list with null values"
include = false

[dc90a09c-5376-449c-a7b3-c2d20d540069]
description = "6 level nested array with null values"
reimplements = "ef1d4790-1b1e-4939-a179-51ace0829dbd"

[85721643-705a-4150-93ab-7ae398e2942d]
description = "all values in nested list are null"
include = false

[51f5d9af-8f7f-4fb5-a156-69e8282cb275]
description = "all values in nested array are null"
reimplements = "85721643-705a-4150-93ab-7ae398e2942d"
4 changes: 4 additions & 0 deletions exercises/practice/flatten-array/flatten_array.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
flatten: (input) ->
error 'implement me'
}
57 changes: 57 additions & 0 deletions exercises/practice/flatten-array/flatten_array_spec.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import flatten from require 'flatten_array'

describe 'flatten-array', ->
it 'empty', ->
input = {}
expected = {}
assert.are.same expected, flatten input

pending 'no nesting', ->
input = {0, 1, 2}
expected = {0, 1, 2}
assert.are.same expected, flatten input

pending 'flattens a nested array', ->
input = {{{}}}
expected = {}
assert.are.same expected, flatten input

pending 'flattens array with just integers present', ->
input = {1, {2, 3, 4, 5, 6, 7}, 8}
expected = {1, 2, 3, 4, 5, 6, 7, 8}
assert.are.same expected, flatten input

pending '5 level nesting', ->
input = {0, 2, {{2, 3}, 8, 100, 4, {{{50}}}}, -2}
expected = {0, 2, 2, 3, 8, 100, 4, 50, -2}
assert.are.same expected, flatten input

pending '6 level nesting', ->
input = {1, {2, {{3}}, {4, {{5}}}, 6, 7}, 8}
expected = {1, 2, 3, 4, 5, 6, 7, 8}
assert.are.same expected, flatten input

pending 'null values are omitted from the final result', ->
input = {1, 2, "null"}
expected = {1, 2}
assert.are.same expected, flatten input

pending 'consecutive null values at the front of the array are omitted from the final result', ->
input = {"null", "null", 3}
expected = {3}
assert.are.same expected, flatten input

pending 'consecutive null values in the middle of the array are omitted from the final result', ->
input = {1, "null", "null", 4}
expected = {1, 4}
assert.are.same expected, flatten input

pending '6 level nested array with null values', ->
input = {0, 2, {{2, 3}, 8, {{100}}, "null", {{}}}, -2}
expected = {0, 2, 2, 3, 8, 100, -2}
assert.are.same expected, flatten input

pending 'all values in nested array are null', ->
input = {"null", {{{}}}, "null", "null", {{}}}
expected = {}
assert.are.same expected, flatten input
Loading