Sha256: f9fe7321b89c35b383b76fa68585b6debd0f54b46844c843e227bcd1580a4bf2

Contents?: true

Size: 460 Bytes

Versions: 396

Compression:

Stored size: 460 Bytes

Contents

(ns binary-search)

(defn middle [alist]
  (-> alist (count) (quot 2)))

(defn search-for
  [elem alist]
  (let [middle (middle alist)
        cur-elem (nth alist middle)]
    (cond
      (= cur-elem elem) middle
      (or (= middle (count alist)) (zero? middle)) (throw (Exception. (format "%s not found in list" elem)))
      (< cur-elem elem) (+ middle (search-for elem (drop middle alist)))
      (> cur-elem elem) (search-for elem (take middle alist)))))

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.179 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.178 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.177 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.176 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.175 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.174 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.173 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.172 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.171 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.170 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.169 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.167 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.166 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.165 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.164 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.163 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.162 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.161 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.160 tracks/clojure/exercises/binary-search/src/example.clj