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.
Merge pull request #47 from LeaveNhA/binary-search-exercise
Add some exercises!
- Loading branch information
Showing
34 changed files
with
808 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,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 allergies) | ||
|
||
(defn allergies []) | ||
|
||
(defn allergic-to? []) |
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,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)) |
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,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
54
exercises/practice/binary-search-tree/.docs/instructions.md
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,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 |
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,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" | ||
} |
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,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 |
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"]}}} |
15 changes: 15 additions & 0 deletions
15
exercises/practice/binary-search-tree/src/binary_search_tree.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,15 @@ | ||
(ns binary-search-tree) | ||
|
||
(defn value []) | ||
|
||
(defn singleton []) | ||
|
||
(defn insert []) | ||
|
||
(defn left []) | ||
|
||
(defn right []) | ||
|
||
(defn to-list []) | ||
|
||
(defn from-list []) |
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,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))))) |
43 changes: 43 additions & 0 deletions
43
exercises/practice/binary-search-tree/test/binary_search_tree_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,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]))))) |
Oops, something went wrong.