-
-
Notifications
You must be signed in to change notification settings - Fork 3
Add flatten-array #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
glennj
wants to merge
1
commit into
exercism:main
Choose a base branch
from
glennj:flatten-array
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+237
−0
Open
Add flatten-array #104
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| return { | ||
| default = { | ||
| ROOT = { '.' } | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
exercises/practice/flatten-array/.docs/instructions.append.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
34
exercises/practice/flatten-array/.meta/spec_generator.moon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
|
||
| 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' | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| flatten: (input) -> | ||
| error 'implement me' | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
json.decodehas three parameters per https://dkolf.de/dkjson-lua/documentation. The third one lets us specify a replacement value besides the defaultnilfor nulls in the JSON. The page documents ajson.nullfor setting an explicit null value. Inbin/generate-spec, we usecanonical_data = json.decode read_file canonical_data_path, but we could instead usecanonical_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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!