Last active
June 9, 2016 10:37
-
-
Save collinalexbell/63f05e7a2cd64c7bfa9ce3023d8bfdd7 to your computer and use it in GitHub Desktop.
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
(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))))) | |
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
(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