Skip to content

Instantly share code, notes, and snippets.

@maruks
Created November 19, 2012 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maruks/4114198 to your computer and use it in GitHub Desktop.
Save maruks/4114198 to your computer and use it in GitHub Desktop.
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