-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
5 changes: 5 additions & 0 deletions
5
exercises/practice/flatten-array/.docs/instructions-append.md
This file contains 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 @@ | ||
# Instructions append | ||
|
||
This exercise is implemented using the built-in [List] datatype, not RawArrays. | ||
|
||
[list]: https://pyret.org/docs/latest/lists.html |
This file contains 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,11 @@ | ||
# Instructions | ||
|
||
Take a nested list and return a single flattened list with all values except nil/null. | ||
|
||
The challenge is to write a function that accepts an arbitrarily-deep nested list-like structure and returns a flattened structure without any nil/null values. | ||
|
||
For example: | ||
|
||
input: [1,[2,3,null,4],[null],5] | ||
|
||
output: [1,2,3,4,5] |
This file contains 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": [ | ||
"BNAndras" | ||
], | ||
"files": { | ||
"solution": [ | ||
"flatten-array.arr" | ||
], | ||
"test": [ | ||
"flatten-array-test.arr" | ||
], | ||
"example": [ | ||
".meta/example.arr" | ||
] | ||
}, | ||
"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 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,22 @@ | ||
use context essentials2020 | ||
|
||
provide: flatten end | ||
|
||
import lists as L | ||
|
||
fun flatten(lst): | ||
lst.foldr(lam(elt, acc): | ||
ask: | ||
| elt == nothing then: | ||
acc | ||
| is-empty(elt) then: | ||
acc | ||
| is-link(elt) then: | ||
result = flatten(elt) | ||
result.append(acc) | ||
| otherwise: | ||
L.push(acc, elt) | ||
end | ||
end, | ||
[list: ]) | ||
end |
This file contains 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,43 @@ | ||
# 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" | ||
|
||
[382c5242-587e-4577-b8ce-a5fb51e385a1] | ||
description = "consecutive null values in the middle of the list are omitted from the final result" | ||
|
||
[ef1d4790-1b1e-4939-a179-51ace0829dbd] | ||
description = "6 level nest list with null values" | ||
|
||
[85721643-705a-4150-93ab-7ae398e2942d] | ||
description = "all values in nested list are null" |
207 changes: 207 additions & 0 deletions
207
exercises/practice/flatten-array/flatten-array-test.arr
This file contains 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,207 @@ | ||
use context essentials2020 | ||
|
||
include file("flatten-array.arr") | ||
|
||
#| | ||
When working offline, all tests except the first one are skipped by default. | ||
Once you get the first test running, unskip the next one until all tests pass locally. | ||
Check the block comment below for further details. | ||
|# | ||
|
||
fun empty-list(): | ||
check "empty": | ||
input = [list: ] | ||
|
||
expected = [list: ] | ||
|
||
flatten(input) is expected | ||
end | ||
end | ||
|
||
fun no-nesting(): | ||
check "no nesting": | ||
input = [list: 0, 1, 2] | ||
|
||
expected = [list: 0, 1, 2] | ||
|
||
flatten(input) is expected | ||
end | ||
end | ||
|
||
fun nested-list(): | ||
check "flattens a nested list": | ||
input = [list: [list: ]] | ||
|
||
expected = [list: ] | ||
|
||
flatten(input) is expected | ||
end | ||
end | ||
|
||
fun numeric-list(): | ||
check "flattens list with just integers present": | ||
input = [list: 1, [list: 2, 3, 4, 5, 6, 7], 8] | ||
|
||
expected = [list: 1, 2, 3, 4, 5, 6, 7, 8] | ||
|
||
flatten(input) is expected | ||
end | ||
end | ||
|
||
fun five-levels(): | ||
check "5 level nesting": | ||
input = [list: | ||
0, | ||
2, | ||
[list: | ||
[list: 2, 3], | ||
8, | ||
100, | ||
4, | ||
[list: | ||
[list: | ||
[list: 50]]]], | ||
-2] | ||
|
||
expected = [list: | ||
0, | ||
2, | ||
2, | ||
3, | ||
8, | ||
100, | ||
4, | ||
50, | ||
-2] | ||
|
||
flatten(input) is expected | ||
end | ||
end | ||
|
||
fun six-levels(): | ||
check "6 level nesting": | ||
input = [list: | ||
1, | ||
[list: | ||
2, | ||
[list: [list: 3]], | ||
[list: | ||
4, | ||
[list: [list: 5]]], | ||
6, | ||
7], | ||
8] | ||
|
||
expected = [list: | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
6, | ||
7, | ||
8] | ||
|
||
flatten(input) is expected | ||
end | ||
end | ||
|
||
fun omit-a-nothing(): | ||
check "nothing values are omitted from the final result": | ||
input = [list: 1, 2, nothing] | ||
|
||
expected = [list: 1, 2] | ||
|
||
flatten(input) is expected | ||
end | ||
end | ||
|
||
fun omit-nothings-from-beginning(): | ||
check "consecutive nothing values at the front of the list are omitted from the final result": | ||
input = [list: nothing, nothing, 3] | ||
|
||
expected = [list: 3] | ||
|
||
flatten(input) is expected | ||
end | ||
end | ||
|
||
fun omit-nothings-from-middle(): | ||
check "consecutive nothing values in the middle of the list are omitted from the final result": | ||
input = [list: 1, nothing, nothing, 4] | ||
|
||
expected = [list: 1, 4] | ||
|
||
flatten(input) is expected | ||
end | ||
end | ||
|
||
fun six-levels-with-nothings(): | ||
check "6 level nest list with nothing values": | ||
input = [list: | ||
0, | ||
2, | ||
[list: | ||
[list: 2, 3], | ||
8, | ||
[list: [list: 100 ]], | ||
nothing, | ||
[list: [list: nothing]]], | ||
-2] | ||
|
||
expected = [list: | ||
0, | ||
2, | ||
2, | ||
3, | ||
8, | ||
100, | ||
-2] | ||
|
||
flatten(input) is expected | ||
end | ||
end | ||
|
||
fun all-nothings(): | ||
check "all values in nested list are nothing": | ||
input = [list: | ||
nothing, | ||
[list: | ||
[list: | ||
[list: nothing]]], | ||
nothing, | ||
nothing, | ||
[list: | ||
[list: | ||
nothing, | ||
nothing], | ||
nothing], | ||
nothing] | ||
|
||
expected = [list: ] | ||
|
||
flatten(input) is expected | ||
end | ||
end | ||
|
||
#| | ||
Code to run each test. Each line corresponds to a test above and whether it should be run. | ||
To mark a test to be run, replace `false` with `true` on that same line after the comma. | ||
test(test-a, true) will be run. test(test-a, false) will be skipped. | ||
|# | ||
|
||
data TestRun: test(run, active) end | ||
|
||
[list: | ||
test(empty-list, true), | ||
test(no-nesting, false), | ||
test(nested-list, false), | ||
test(numeric-list, false), | ||
test(five-levels, false), | ||
test(six-levels, false), | ||
test(omit-a-nothing, false), | ||
test(omit-nothings-from-beginning, false), | ||
test(omit-nothings-from-middle, false), | ||
test(six-levels-with-nothings, false), | ||
test(all-nothings, false) | ||
].each(lam(t): when t.active: t.run() end end) |
This file contains 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 @@ | ||
use context essentials2020 | ||
|
||
provide: flatten end | ||
|
||
fun flatten(lst): | ||
raise("please implement the flatten function") | ||
end |