Add flatten-array exercise (#159)

This commit is contained in:
Erik Schierboom
2024-04-29 11:57:31 +02:00
committed by GitHub
parent 1988a73e78
commit e53aee8421
8 changed files with 314 additions and 0 deletions

View File

@@ -348,6 +348,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "flatten-array",
"name": "Flatten Array",
"uuid": "ddf5e66f-73c6-4fcf-9ed9-dde95d896d4a",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},

View File

@@ -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 take an arbitrarily-deep nested list-like structure and produce a flattened structure without any nil/null values.
For example:
input: [1,[2,3,null,4],[null],5]
output: [1,2,3,4,5]

View File

@@ -0,0 +1,19 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"flatten-array.8th"
],
"test": [
"test.8th"
],
"example": [
".meta/example.8th"
]
},
"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"
}

View File

@@ -0,0 +1,4 @@
: flatten \ a -- a
array? if ( flatten a:+ ) a:new a:reduce ;then
null? if drop [] else a:new swap a:push then
;

View File

@@ -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"

View File

@@ -0,0 +1,3 @@
: flatten \ a -- a
;

View File

@@ -0,0 +1,162 @@
needs console/loaded
\ -----------------------------------------------------------------
ns: test
-1 var, test-count
var tests-passed
var tests-failed
var tests-skipped
true var, run-test
\ Some utility words
: array-insens-dot \ x -- \\ removes leading space on stringified arrays
dup >kind ns:a n:=
if
>s s:trim
then
.
;
: test-passed \ s x x -- \\ test name, expected value, actual value
2drop
1 tests-passed n:+!
con:green con:onBlack . space " ... OK" . con:white con:onBlack cr
;
: test-skipped \ s --
1 tests-skipped n:+!
con:cyan con:onBlack . space " ... SKIPPED" . con:white con:onBlack cr
;
: test-failed \ s x x -- \\ test name, expected value, actual value
1 tests-failed n:+!
rot
con:red con:onBlack . space " ... FAIL" . con:white con:onBlack cr
" Actual: «" . array-insens-dot "»" . cr
" Expected: «" . array-insens-dot "»" . cr cr
;
: isword? \ x -- x f
dup >kind ns:w n:=
;
: run-test? \ -- T
run-test @ if true else "RUN_ALL_TESTS" getenv n:>bool then
;
\ Num passed + num skipped + num failed should == num tests
: all-tests-run? \ -- T
tests-passed @ tests-skipped @ tests-failed @ n:+ n:+
test-count @ n:=
;
\ returns true if x is a date, false otherwise
: date? \ x -- x T
dup >kind ns:d n:=
;
\ adapted from 8th forum -- https://8th-dev.com/forum/index.php/topic,2745.0.html
: eq? \ x x -- T
\ are the items the same kind?
2dup >kind swap >kind n:=
!if 2drop false ;then
\ same kind: try different comparators
number? if n:= ;then
string? if s:= ;then
array? if ' eq? a:= 2nip ;then
map? if ' eq? m:= 2nip ;then
date? if d:= ;then
\ otherwise fall back to 'lazy evaluation'
l: =
;
: check-depth \ ... n -- ...
dup>r
n:1+ depth n:=
!if
con:red con:onBlack
"PANIC: expected stack depth to be " . r> . cr
"Stack is:" . cr
.s cr
255 die
then
rdrop
;
\ -----------------------------------------------------------------
\ status report at end of run
( all-tests-run?
!if con:red con:onBlack "... FAIL - not all tests completed" . con:white con:onBlack cr then
) onexit
\ Print a summary of the tests run
( con:white con:onBlack
test-count @ . space "tests planned - " .
tests-passed @ . space "passed - " .
tests-skipped @ . space "skipped - " .
tests-failed @ . space "failed" . cr
) onexit
\ -----------------------------------------------------------------
\ The public-facing words
\ -----------------------------------------------------------------
: equal? \ s x w -- | s w x --
run-test? !if 2drop test-skipped ;; then
isword? !if swap then
w:exec
3 check-depth
2dup \ so test-failed can show actual and expected
eq? if test-passed else test-failed then
;
: true? \ s w --
run-test? !if drop test-skipped ;; then
w:exec
2 check-depth
true swap dup \ so test-failed can show actual and expected
if test-passed else test-failed then
;
: false? \ s w --
run-test? !if drop test-skipped ;; then
w:exec
2 check-depth
false swap dup \ so test-failed can show actual and expected
!if test-passed else test-failed then
;
: null? \ s w --
run-test? !if drop test-skipped ;; then
w:exec
2 check-depth
null swap dup \ so test-failed can show actual and expected
G:null? nip if test-passed else test-failed then
;
: SKIP-REST-OF-TESTS false run-test ! ;
: tests \ n --
test-count !
;
\ Set the exit status:
\ 0 = all OK
\ 1 = not all tests were run (some error occurred)
\ 2 = some tests failed
: end-of-tests \ --
all-tests-run?
if
tests-failed @ 0 n:= if 0 else 2 then
else
1
then
die
;

View File

@@ -0,0 +1,64 @@
"flatten-array.8th" f:include
needs exercism/test
with: test
11 tests
"empty"
( [] flatten )
[]
equal?
SKIP-REST-OF-TESTS
"no nesting"
( [0, 1, 2] flatten )
[0, 1, 2]
equal?
"flattens a nested array"
( [[[]]] flatten )
[]
equal?
"flattens array with just integers present"
( [1, [2, 3, 4, 5, 6, 7], 8] flatten )
[1, 2, 3, 4, 5, 6, 7, 8]
equal?
"5 level nesting"
( [0, 2, [[2, 3], 8, 100, 4, [[[50]]]], -2] flatten )
[0, 2, 2, 3, 8, 100, 4, 50, -2]
equal?
"6 level nesting"
( [1, [2, [[3]], [4, [[5]]], 6, 7], 8] flatten )
[1, 2, 3, 4, 5, 6, 7, 8]
equal?
"null values are omitted from the final result"
( [1, 2, null] flatten )
[1, 2]
equal?
"consecutive null values at the front of the list are omitted from the final result"
( [null, null, 3] flatten )
[3]
equal?
"consecutive null values in the middle of the list are omitted from the final result"
( [1, null, null, 4] flatten )
[1, 4]
equal?
"6 level nest list with null values"
( [0, 2, [[2, 3], 8, [[100]], null, [[null]]], -2] flatten )
[0, 2, 2, 3, 8, 100, -2]
equal?
"all values in nested list are null"
( [null, [[[null]]], null, null, [[null, null], null], null] flatten )
[]
equal?
end-of-tests
;with