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

Commit

Permalink
Add flatten-array exercise (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored May 6, 2023
1 parent 10787e4 commit eed9ed4
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,16 @@
"difficulty": 5,
"topics": null
},
{
"slug": "flatten-array",
"name": "Flatten Array",
"uuid": "05847b82-2bcb-4a14-b189-db2876c511ba",
"practices": [],
"prerequisites": [
"vectors"
],
"difficulty": 3
},
{
"slug": "etl",
"name": "Etl",
Expand Down
11 changes: 11 additions & 0 deletions exercises/practice/flatten-array/.docs/instructions.md
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]
19 changes: 19 additions & 0 deletions exercises/practice/flatten-array/.meta/config.json
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"
}
9 changes: 9 additions & 0 deletions exercises/practice/flatten-array/.meta/src/example.cljs
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?)))
21 changes: 21 additions & 0 deletions exercises/practice/flatten-array/.meta/tests.toml
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"
10 changes: 10 additions & 0 deletions exercises/practice/flatten-array/deps.edn
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"]}}}
5 changes: 5 additions & 0 deletions exercises/practice/flatten-array/src/flatten_array.cljs
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 exercises/practice/flatten-array/test/flatten_array_test.cljs
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])))))

0 comments on commit eed9ed4

Please sign in to comment.