Created
November 19, 2012 21:41
-
-
Save maruks/4114198 to your computer and use it in GitHub Desktop.
London Code Dojo Late Nov
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
(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