This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
1 parent
10787e4
commit eed9ed4
Showing
8 changed files
with
113 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
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": [ | ||
"ErikSchierboom" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/flatten_array.cljs" | ||
], | ||
"test": [ | ||
"test/flatten_array_test.cljs" | ||
], | ||
"example": [ | ||
".meta/src/example.cljs" | ||
] | ||
}, | ||
"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,9 @@ | ||
(ns flatten-array | ||
(:refer-clojure :exclude [flatten])) | ||
|
||
(defn flatten [s] | ||
(->> s | ||
(tree-seq sequential? seq) | ||
rest | ||
(remove sequential?) | ||
(remove nil?))) |
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,21 @@ | ||
# This is an auto-generated file. Regular comments will be removed when this | ||
# file is regenerated. Regenerating will not touch any manually added keys, | ||
# so comments can be added in a "comment" key. | ||
|
||
[d268b919-963c-442d-9f07-82b93f1b518c] | ||
description = "no nesting" | ||
|
||
[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" | ||
|
||
[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" |
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,10 @@ | ||
{:deps | ||
{org.clojure/clojure {:mvn/version "1.10.1"} | ||
org.clojure/clojurescript {:mvn/version "1.10.773"}} | ||
|
||
:aliases | ||
{:test | ||
{:extra-paths ["test"] | ||
:extra-deps | ||
{olical/cljs-test-runner {:mvn/version "3.8.0"}} | ||
:main-opts ["-m" "cljs-test-runner.main"]}}} |
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 @@ | ||
(ns flatten-array) | ||
|
||
(defn flatten [arr] ;; <- arglist goes here | ||
;; your code goes here | ||
) |
28 changes: 28 additions & 0 deletions
28
exercises/practice/flatten-array/test/flatten_array_test.cljs
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,28 @@ | ||
(ns flatten-array-test | ||
(:require [clojure.test :refer [deftest is testing]] | ||
[flatten-array])) | ||
|
||
(deftest flattens-array-of-ints | ||
(testing "flattens array with just integers present" | ||
(is (= [1 2 3 4 5 6 7 8] | ||
(flatten-array/flatten [1 [2 3 4 5 6 7] 8]))))) | ||
|
||
(deftest five-level-nesting | ||
(testing "5 level nested list" | ||
(is (= [0 2 2 3 8 100 4 50 -2] | ||
(flatten-array/flatten [0 2 [[2 3] 8 100 4 [[[50]]]] -2]))))) | ||
|
||
(deftest six-level-nesting | ||
(testing "6 level nested list" | ||
(is (= [1 2 3 4 5 6 7 8] | ||
(flatten-array/flatten [1 [2 [[3]] [4 [[5]]] 6 7] 8]))))) | ||
|
||
(deftest six-level-nested-with-nils | ||
(testing "6 level nested list with nil values" | ||
(is (= [0 2 2 3 8 100 -2] | ||
(flatten-array/flatten [0 2 [[2 3] 8 [[100]] nil [[nil]]] -2]))))) | ||
|
||
(deftest all-nils-list | ||
(testing "All values in nested list are nil" | ||
(is (empty? | ||
(flatten-array/flatten [nil [[[nil]]] nil nil [[nil nil] nil] nil]))))) |