public
Created

London Code Dojo Late Nov

  • Download Gist
fibo_dojo.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
(ns sandbox.fibo-dojo)
 
(def fib (cons 1 (cons 2 (lazy-seq (map + (rest fib) fib)))))
 
(def indexes (iterate inc 0))
 
(defn cons-one? [xs prev]
(if (seq xs)
(if (and (= 1 prev) (= (first xs) prev)) true
(cons-one? (rest xs) (first xs)))))
 
(defn encode [sum idxs acc target]
(cond (= sum target) (if (cons-one? acc 0) '() (cons -1 acc))
(or (> sum target) (> (first idxs) target)) '()
:else (concat (encode sum (rest idxs) (cons 0 acc) target)
(encode (+ sum (nth fib (first idxs))) (rest idxs) (cons 1 acc) target))))
 
(defn enc [num]
(filter #(not (= '(-1) %)) (partition-by #(= -1 %) (encode 0 indexes '() num))))
 
(defn print-table [n]
(let [xs (drop 1 (take n indexes))
res (map #(cons % (enc %)) xs)]
(doseq [r res] (println r) )))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.