Created
August 2, 2016 16:17
-
-
Save brancusi/d1ce254d5b04abaecacaa1d49e61b2d4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 roman-numerals | |
| (:require [clojure.string :as s])) | |
| (def ^:private romans {1000 \M, 500 \D, 100 \C, 50 \L, 10 \X, 5 \V, 1 \I}) | |
| (defn gen-char | |
| [num] | |
| (let [tuple (first (filter #(>= 0 (- (first %) num)) romans))] | |
| [(last tuple) (- num (first tuple))])) | |
| (gen-char 6) | |
| (defn dumb-to-smart | |
| [str] | |
| ()) | |
| (defn numerals | |
| ([x](numerals x "")) | |
| ([x seed] | |
| (if (<= x 0) | |
| seed | |
| (let [tup (gen-char x)] | |
| (numerals (last tup) (str seed (first tup))))))) | |
| (numerals 1945) | |
| ; MDCCCCXXXXV | |
| ; = | |
| ; MCMXLV | |
| ; | |
| ; L | |
| (defn is-same | |
| [col] | |
| (= 1 (count (into #{} col)))) | |
| (defn apply-first-rule | |
| [params] | |
| body) | |
| (defn convert-to | |
| ([dumb-str]) | |
| ([dumb-str cur-index])) | |
| (convert-to "MCCCC") | |
| ; CM = 1900 | |
| ; | |
| ; body) | |
| ; | |
| ; (numerals 6) | |
| ; | |
| ; (defn foo | |
| ; [num] | |
| ; (loop [i 0] | |
| ; (when (< i 5) | |
| ; (println i) | |
| ; (recur (inc i))))) | |
| ; | |
| ; | |
| ; 1. | |
| ; (defn not-nil | |
| ; [val] | |
| ; (not= val nil)) | |
| ; | |
| ; (defn get-by-key | |
| ; [key] | |
| ; (romans key)) | |
| ; | |
| ; (defn build-chars | |
| ; [vals] | |
| ; (->> | |
| ; (map get-by-key vals) | |
| ; (filter not-nil) | |
| ; (s/join))) | |
| ; (build-chars [10, 100]) | |
| ; If we round to the most significant number then we can first | |
| ; match against the add numerals. If there is a clean match we move on | |
| ; Maybe that should be the first match | |
| ; Otherwise 1000-500 matches to CM which should just be D | |
| ; I think this loop is somehow subtracting out to 1 and then just looping | |
| ; (defn match | |
| ; [n] | |
| ; (loop [add (keys romans) sub (reverse (keys romans))] | |
| ; (println add sub) | |
| ; (cond | |
| ; (>= n (first add)) [(build-chars [(first add)]) (- n (first add))] | |
| ; (>= n (- (first add) (first sub))) [(build-chars [(first sub) (first add)]) (- n (- (first add) (first sub)))] | |
| ; :else (if (empty? (rest sub)) | |
| ; (if (empty? (rest add)) | |
| ; ["" 0] | |
| ; (recur (rest add) (reverse (keys romans)))) | |
| ; (recur add (rest sub)))))) | |
| ; | |
| ; (defn numerals | |
| ; [n] | |
| ; (loop [chars "" remaining n] | |
| ; (if (> remaining 0) | |
| ; (let [yo (match remaining)] | |
| ; (recur (str chars (first yo)) (last yo))) | |
| ; chars))) | |
| ; (reduce (fn [acc {k v}] (build-chars n k v romans)) "" romans)) | |
| ; (defn build-chars | |
| ; [num factor roman] | |
| ; (s/join (map #((str roman)) (range (int (/ num factor)))))) | |
| ; | |
| ; (defn numerals | |
| ; [num] | |
| ; (reduce (fn [acc [k v]] (str acc (build-chars num k v))) "" romans)) | |
| ; (numerals 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment