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

Commit

Permalink
Merge pull request #47 from LeaveNhA/binary-search-exercise
Browse files Browse the repository at this point in the history
Add some exercises!
  • Loading branch information
LeaveNhA committed Apr 22, 2021
2 parents 7700a33 + 04bb2ef commit 0135bc5
Show file tree
Hide file tree
Showing 34 changed files with 808 additions and 0 deletions.
49 changes: 49 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,55 @@
"prerequisites": [],
"slug": "clock",
"name": "Clock"
},
{
"slug": "allergies",
"name": "Allergies",
"uuid": "0793ac96-5280-4f0d-a08a-aa57ad9b9946",
"practices": [],
"prerequisites": [],
"difficulty": 4,
"topics": null
},
{
"slug": "matching-brackets",
"name": "Matching Brackets",
"uuid": "38eabf83-ebcb-4385-b409-1b2cc0540138",
"practices": [],
"prerequisites": [],
"difficulty": 7,
"topics": null
},
{
"slug": "binary-search",
"name": "Binary Search",
"uuid": "5463a96a-c357-4847-ab86-e35464a01a49",
"practices": [],
"prerequisites": [],
"difficulty": 3,
"topics": null
},
{
"slug": "binary-search-tree",
"name": "Binary Search Tree",
"uuid": "202b0eba-705c-4654-804b-b086493b6901",
"practices": [],
"prerequisites": [],
"difficulty": 3,
"topics": [
"data structures"
]
},
{
"slug": "pascals-triangle",
"name": "Pascals Triangle",
"uuid": "e4e8d5a7-26ce-4153-899c-6bea61c8677d",
"practices": [],
"prerequisites": [],
"difficulty": 3,
"topics": [
"math"
]
}
]
},
Expand Down
10 changes: 10 additions & 0 deletions exercises/practice/allergies/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/allergies/src/allergies.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns allergies)

(defn allergies [])

(defn allergic-to? [])
46 changes: 46 additions & 0 deletions exercises/practice/allergies/src/example.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(ns allergies)

(def ^:private allergy-number
{:eggs 1
:peanuts 2
:shellfish 4
:strawberries 8
:tomatoes 16
:chocolate 32
:pollen 64
:cats 128})

(defn- total-allergic-points [allergy-number]
(apply + (map second allergy-number)))

(defn- allergic->name [ans]
(map first ans))

(defn- check-allergy [x y]
(> (bit-and x y)
0))

(defn- allergic? [an allergy-number]
(filter #(check-allergy an (second %))
allergy-number))

(defn- validate-allergy-number [an tan]
(if (> an tan)
(bit-and an tan)
an))

(defn allergies [an]
(-> an
(validate-allergy-number (total-allergic-points allergy-number))
(allergic? allergy-number)
allergic->name
vec))

(defn allergic-to? [an at]
(-> (partial = at)
(filter
(allergies an))
vec
count
zero?
not))
47 changes: 47 additions & 0 deletions exercises/practice/allergies/test/allergies_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
(ns allergies-test
(:require [allergies]
[cljs.test :refer [deftest is testing] :as t :include-macros true]))

;; TODO: [LeaveNhA] add testing calls for a better view on testing!

(deftest no-allergies-at-all
(is (= [] (allergies/allergies 0))))

(deftest allergic-to-just-eggs
(is (= [:eggs] (allergies/allergies 1))))

(deftest allergic-to-just-peanuts
(is (= [:peanuts] (allergies/allergies 2))))

(deftest allergic-to-just-strawberries
(is (= [:strawberries] (allergies/allergies 8))))

(deftest allergic-to-eggs-and-peanuts
(is (= [:eggs :peanuts] (allergies/allergies 3))))

(deftest allergic-to-more-than-eggs-but-not-peanuts
(is (= [:eggs :shellfish] (allergies/allergies 5))))

(deftest allergic-to-lots-of-stuff
(is (= [:strawberries :tomatoes :chocolate :pollen :cats]
(allergies/allergies 248))))

(deftest allergic-to-everything
(is (= [:eggs :peanuts :shellfish :strawberries
:tomatoes :chocolate :pollen :cats]
(allergies/allergies 255))))

(deftest no-allergies-means-not-allergic
(is (not (allergies/allergic-to? 0 :peanuts)))
(is (not (allergies/allergic-to? 0 :cats)))
(is (not (allergies/allergic-to? 0 :strawberries))))

(deftest is-allergic-to-eggs
(is (allergies/allergic-to? 1 :eggs)))

(deftest allergic-to-eggs-in-addition-to-other-stuff
(is (allergies/allergic-to? 5 :eggs)))

(deftest ignore-non-allergen-score-parts
(is (= [:eggs :shellfish :strawberries :tomatoes :chocolate :pollen :cats]
(allergies/allergies 509))))
54 changes: 54 additions & 0 deletions exercises/practice/binary-search-tree/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Instructions

Insert and search for numbers in a binary tree.

When we need to represent sorted data, an array does not make a good
data structure.

Say we have the array `[1, 3, 4, 5]`, and we add 2 to it so it becomes
`[1, 3, 4, 5, 2]` now we must sort the entire array again! We can
improve on this by realizing that we only need to make space for the new
item `[1, nil, 3, 4, 5]`, and then adding the item in the space we
added. But this still requires us to shift many elements down by one.

Binary Search Trees, however, can operate on sorted data much more
efficiently.

A binary search tree consists of a series of connected nodes. Each node
contains a piece of data (e.g. the number 3), a variable named `left`,
and a variable named `right`. The `left` and `right` variables point at
`nil`, or other nodes. Since these other nodes in turn have other nodes
beneath them, we say that the left and right variables are pointing at
subtrees. All data in the left subtree is less than or equal to the
current node's data, and all data in the right subtree is greater than
the current node's data.

For example, if we had a node containing the data 4, and we added the
data 2, our tree would look like this:

4
/
2

If we then added 6, it would look like this:

4
/ \
2 6

If we then added 3, it would look like this

4
/ \
2 6
\
3

And if we then added 1, 5, and 7, it would look like this

4
/ \
/ \
2 6
/ \ / \
1 3 5 7
17 changes: 17 additions & 0 deletions exercises/practice/binary-search-tree/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"blurb": "Insert and search for numbers in a binary tree.",
"authors": [],
"files": {
"solution": [
"src/{snake_slug}.cljs"
],
"test": [
"test/{snake_slug}_test.cljs"
],
"example": [
"src/example.cljs"
]
},
"source": "Josh Cheek",
"source_url": "https://twitter.com/josh_cheek"
}
31 changes: 31 additions & 0 deletions exercises/practice/binary-search-tree/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[canonical-tests]

# data is retained
"e9c93a78-c536-4750-a336-94583d23fafa" = true

# smaller number at left node
"7a95c9e8-69f6-476a-b0c4-4170cb3f7c91" = true

# same number at left node
"22b89499-9805-4703-a159-1a6e434c1585" = true

# greater number at right node
"2e85fdde-77b1-41ed-b6ac-26ce6b663e34" = true

# can create complex tree
"dd898658-40ab-41d0-965e-7f145bf66e0b" = true

# can sort single number
"9e0c06ef-aeca-4202-b8e4-97f1ed057d56" = true

# can sort if second number is smaller than first
"425e6d07-fceb-4681-a4f4-e46920e380bb" = true

# can sort if second number is same as first
"bd7532cc-6988-4259-bac8-1d50140079ab" = true

# can sort if second number is greater than first
"b6d1b3a5-9d79-44fd-9013-c83ca92ddd36" = true

# can sort complex tree
"d00ec9bd-1288-4171-b968-d44d0808c1c8" = true
10 changes: 10 additions & 0 deletions exercises/practice/binary-search-tree/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"]}}}
15 changes: 15 additions & 0 deletions exercises/practice/binary-search-tree/src/binary_search_tree.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(ns binary-search-tree)

(defn value [])

(defn singleton [])

(defn insert [])

(defn left [])

(defn right [])

(defn to-list [])

(defn from-list [])
25 changes: 25 additions & 0 deletions exercises/practice/binary-search-tree/src/example.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(ns binary-search-tree)

(defn value [node] (first node))
(defn left [node] (second node))
(defn right [node] (last node))
(defn singleton [n] [n nil nil])

(defn insert [v node]
(if
(empty? node)
(singleton v)
(let [x (value node)]
(if
(>= x v)
[x (insert v (left node)) (right node)]
[x (left node) (insert v (right node))]))))

(defn from-list [xs]
(reduce #(insert %2 %1) nil xs))

(defn to-list [node]
(if
(empty? node)
node
(concat (to-list (left node)) [(value node)] (to-list (right node)))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(ns binary-search-tree-test
(:require [cljs.test :refer [deftest testing is] :as t :include-macros true]
[binary-search-tree :as bst]))

(deftest data-is-retained
(is (= 4 (bst/value (bst/singleton 4)))))

(deftest inserting-less
(let [t (bst/insert 2 (bst/singleton 4))]
(is (= 4 (bst/value t)))
(is (= 2 (bst/value (bst/left t))))))

(deftest inserting-same
(let [t (bst/insert 4 (bst/singleton 4))]
(is (= 4 (bst/value t)))
(is (= 4 (bst/value (bst/left t))))))

(deftest inserting-right
(let [t (bst/insert 5 (bst/singleton 4))]
(is (= 4 (bst/value t)))
(is (= 5 (bst/value (bst/right t))))))

(deftest complex-tree
(let [t (bst/from-list [4 2 6 1 3 7 5])]
(is (= 4 (bst/value t)))
(is (= 2 (bst/value (bst/left t))))
(is (= 1 (bst/value (bst/left (bst/left t)))))
(is (= 3 (bst/value (bst/right (bst/left t)))))
(is (= 6 (bst/value (bst/right t))))
(is (= 5 (bst/value (bst/left (bst/right t)))))
(is (= 7 (bst/value (bst/right (bst/right t)))))))

(deftest iterating-one-element
(is (= [4] (bst/to-list (bst/singleton 4)))))

(deftest iterating-over-smaller-element
(is (= [2 4] (bst/to-list (bst/from-list [4 2])))))

(deftest iterating-over-larger-element
(is (= [4 5] (bst/to-list (bst/from-list [4 5])))))

(deftest iterating-over-complex-tree
(is (= (range 1 8) (bst/to-list (bst/from-list [4 2 1 3 6 7 5])))))
Loading

0 comments on commit 0135bc5

Please sign in to comment.