Advertisement
proffreda

Clojure: Binary Reflected Gray Codes

Feb 25th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn towers [n]
  2.     (cond
  3.       (= n 1) (list 1)
  4.        :else
  5.          (apply conj (towers (dec n)) n (towers (dec n)))))
  6.  
  7. (defn gray [n]
  8.      (cond
  9.         (= n 1) (list [0] [1])
  10.         :else
  11.         (let [firsthalf  (map #(conj  % 0) (gray (dec n)) )
  12.               secondhalf (map #(conj  % 1) (reverse (gray (dec n))))  ]
  13.           (concat  firsthalf secondhalf))))
  14.  
  15. (defn rank [code]
  16.       (cond
  17.        (= code [0]) 0
  18.        (= code [1]) 1
  19.        :else
  20.         (cond
  21.          (= 0 (last code))  (rank (vec (drop-last code)))
  22.            :else
  23.            (dec (- (Math/pow 2 (count code)) (rank (vec (drop-last code))))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement