Skip to content
This repository was archived by the owner on May 30, 2023. It is now read-only.

Commit eed9ed4

Browse files
Add flatten-array exercise (#124)
1 parent 10787e4 commit eed9ed4

File tree

8 files changed

+113
-0
lines changed

8 files changed

+113
-0
lines changed

config.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,16 @@
315315
"difficulty": 5,
316316
"topics": null
317317
},
318+
{
319+
"slug": "flatten-array",
320+
"name": "Flatten Array",
321+
"uuid": "05847b82-2bcb-4a14-b189-db2876c511ba",
322+
"practices": [],
323+
"prerequisites": [
324+
"vectors"
325+
],
326+
"difficulty": 3
327+
},
318328
{
319329
"slug": "etl",
320330
"name": "Etl",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Instructions
2+
3+
Take a nested list and return a single flattened list with all values except nil/null.
4+
5+
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.
6+
7+
For example:
8+
9+
input: [1,[2,3,null,4],[null],5]
10+
11+
output: [1,2,3,4,5]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"ErikSchierboom"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/flatten_array.cljs"
8+
],
9+
"test": [
10+
"test/flatten_array_test.cljs"
11+
],
12+
"example": [
13+
".meta/src/example.cljs"
14+
]
15+
},
16+
"blurb": "Take a nested list and return a single list with all values except nil/null.",
17+
"source": "Interview Question",
18+
"source_url": "https://reference.wolfram.com/language/ref/Flatten.html"
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(ns flatten-array
2+
(:refer-clojure :exclude [flatten]))
3+
4+
(defn flatten [s]
5+
(->> s
6+
(tree-seq sequential? seq)
7+
rest
8+
(remove sequential?)
9+
(remove nil?)))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[d268b919-963c-442d-9f07-82b93f1b518c]
6+
description = "no nesting"
7+
8+
[c84440cc-bb3a-48a6-862c-94cf23f2815d]
9+
description = "flattens array with just integers present"
10+
11+
[d3d99d39-6be5-44f5-a31d-6037d92ba34f]
12+
description = "5 level nesting"
13+
14+
[d572bdba-c127-43ed-bdcd-6222ac83d9f7]
15+
description = "6 level nesting"
16+
17+
[ef1d4790-1b1e-4939-a179-51ace0829dbd]
18+
description = "6 level nest list with null values"
19+
20+
[85721643-705a-4150-93ab-7ae398e2942d]
21+
description = "all values in nested list are null"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{:deps
2+
{org.clojure/clojure {:mvn/version "1.10.1"}
3+
org.clojure/clojurescript {:mvn/version "1.10.773"}}
4+
5+
:aliases
6+
{:test
7+
{:extra-paths ["test"]
8+
:extra-deps
9+
{olical/cljs-test-runner {:mvn/version "3.8.0"}}
10+
:main-opts ["-m" "cljs-test-runner.main"]}}}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(ns flatten-array)
2+
3+
(defn flatten [arr] ;; <- arglist goes here
4+
;; your code goes here
5+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(ns flatten-array-test
2+
(:require [clojure.test :refer [deftest is testing]]
3+
[flatten-array]))
4+
5+
(deftest flattens-array-of-ints
6+
(testing "flattens array with just integers present"
7+
(is (= [1 2 3 4 5 6 7 8]
8+
(flatten-array/flatten [1 [2 3 4 5 6 7] 8])))))
9+
10+
(deftest five-level-nesting
11+
(testing "5 level nested list"
12+
(is (= [0 2 2 3 8 100 4 50 -2]
13+
(flatten-array/flatten [0 2 [[2 3] 8 100 4 [[[50]]]] -2])))))
14+
15+
(deftest six-level-nesting
16+
(testing "6 level nested list"
17+
(is (= [1 2 3 4 5 6 7 8]
18+
(flatten-array/flatten [1 [2 [[3]] [4 [[5]]] 6 7] 8])))))
19+
20+
(deftest six-level-nested-with-nils
21+
(testing "6 level nested list with nil values"
22+
(is (= [0 2 2 3 8 100 -2]
23+
(flatten-array/flatten [0 2 [[2 3] 8 [[100]] nil [[nil]]] -2])))))
24+
25+
(deftest all-nils-list
26+
(testing "All values in nested list are nil"
27+
(is (empty?
28+
(flatten-array/flatten [nil [[[nil]]] nil nil [[nil nil] nil] nil])))))

0 commit comments

Comments
 (0)