Skip to content

Instantly share code, notes, and snippets.

@collinalexbell
Last active June 9, 2016 10:37
Show Gist options
  • Save collinalexbell/63f05e7a2cd64c7bfa9ce3023d8bfdd7 to your computer and use it in GitHub Desktop.
Save collinalexbell/63f05e7a2cd64c7bfa9ce3023d8bfdd7 to your computer and use it in GitHub Desktop.
(defn enumerate-bits [n]
(if (= n 1)
;base case, n = 1
[[0] [1]]
;normal-case, n > 1
;foreach bit string in enumerate-bits (- n 1) create 2 new bitstrings conjing - and 1
(reduce
(fn [strings assembled-string]
(conj strings
(conj assembled-string 0)
(conj assembled-string 1)))
[]
(enumerate-bits (- n 1)))))
(use '[clojure.test :as c-test])
(c-test/deftest test-enumerate-bits []
(let [correct-vals
[[0 0 0 0 0]
[0 0 0 0 1]
[0 0 0 1 0]
[0 0 0 1 1]
[0 0 1 0 0]
[0 0 1 0 1]
[0 0 1 1 0]
[0 0 1 1 1]
[0 1 0 0 0]
[0 1 0 0 1]
[0 1 0 1 0]
[0 1 0 1 1]
[0 1 1 0 0]
[0 1 1 0 1]
[0 1 1 1 0]
[0 1 1 1 1]
[1 0 0 0 0]
[1 0 0 0 1]
[1 0 0 1 0]
[1 0 0 1 1]
[1 0 1 0 0]
[1 0 1 0 1]
[1 0 1 1 0]
[1 0 1 1 1]
[1 1 0 0 1]
[1 1 0 0 1]
[1 1 0 1 0]
[1 1 0 1 1]
[1 1 1 0 0]
[1 1 1 0 1]
[1 1 1 1 0]
[1 1 1 1 1]]]
(is (= (enumerate-bits 5)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment