Skip to content

Instantly share code, notes, and snippets.

@miniharryc
Created August 23, 2019 23:32
Show Gist options
  • Save miniharryc/59d072369b2323c226330c2a4e288585 to your computer and use it in GitHub Desktop.
Save miniharryc/59d072369b2323c226330c2a4e288585 to your computer and use it in GitHub Desktop.
Code Kata1 in Clojure
(defn _midpoint [vec]
(quot (+ (.start vec) (.end vec)) 2))
(defn _chop [item list]
(cond
(empty? list) -1
(= 1 (count list) ) (if (= item (first list)) (.start list) -1)
:else (let [mid (_midpoint list)]
(let [val (get (.v list) mid)]
(cond
(= item val) mid
(< item val) (_chop item (subvec list (.start list) mid))
(> item val) (_chop item (subvec list (inc mid))))
))))
(defn chop [item list] (
_chop item (subvec list 0)))
@miniharryc
Copy link
Author

passes all these tests:

(deftest test-chop
  (is (= -1 (chop 3 [])))
  (is (= -1 (chop 3 [ 1 ])))
  (is (= 0  (chop 1 [ 1 ])))
  (is (= 0  (chop 1 [1 3 5])))
  (is (= 1  (chop 3 [1 3 5])))
  (is (= 2  (chop 5 [1 3 5])))
  (is (= -1 (chop 0 [1 3 5])))
  (is (= -1 (chop 2 [1 3 5])))
  (is (= -1 (chop 4 [1 3 5])))
  (is (= -1 (chop 6 [1 3 5])))
  (is (= 0  (chop 1 [1 3 5 7])))
  (is (= 1  (chop 3 [1 3 5 7])))
  (is (= 2  (chop 5 [1 3 5 7])))
  (is (= 3  (chop 7 [1 3 5 7])))
  (is (= -1 (chop 0 [1 3 5 7])))
  (is (= -1 (chop 2 [1 3 5 7])))
  (is (= -1 (chop 4 [1 3 5 7])))
  (is (= -1 (chop 6 [1 3 5 7])))
  (is (= -1 (chop 8 [1 3 5 7])))
  )

@miniharryc
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment