Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
London Code Dojo Late Nov
(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) )))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment