Skip to content

Instantly share code, notes, and snippets.

@ulsa
Created August 4, 2012 17:04
Show Gist options
  • Save ulsa/3258784 to your computer and use it in GitHub Desktop.
Save ulsa/3258784 to your computer and use it in GitHub Desktop.
Decimal to Roman Numeral converter
(let [lst (filter odd? [1 2 3 4 5])
fst (first lst)
snd (second lst)]
(println fst snd))
=> 1 3
(filter #(>= 6 (first %)) mapping)
=> ([5 "V"] [4 "IV"] [1 "I"])
(filter #(>= 4 (first %)) mapping)
=> ([4 "IV"] [1 "I"])
(filter odd? [1 2 3 4 5])
=> (1 3 5)
(fn [x]
(>= i (first x)))
#(>= i (first %))
{10 "X" 9 "IX" 5 "V" 4 "IV" 1 "I"}
=> {1 "I", 10 "X", 4 "IV", 5 "V", 9 "IX"}
(sorted-map 10 "X" 9 "IX" 5 "V" 4 "IV" 1 "I")
=> {1 "I", 4 "IV", 5 "V", 9 "IX", 10 "X"}
(sorted-map-by > 10 "X" 9 "IX" 5 "V" 4 "IV" 1 "I")
=> {10 "X", 9 "IX", 5 "V", 4 "IV", 1 "I"}
(def mapping (sorted-map-by > 1000 "M" 900 "CM" 500 "D" 400 "CD"
100 "C" 90 "XC" 50 "L" 40 "XL"
10 "X" 9 "IX" 5 "V" 4 "IV" 1 "I"))
(defn roman-numeral [i]
(loop [i i s ""]
(if (zero? i)
s
(let [[[n c]] (filter #(>= i (first %)) mapping)]
(recur (- i n) (str s c))))))
(seq (sorted-map-by > 10 "X" 9 "IX" 5 "V" 4 "IV" 1 "I"))
=> ([10 "X"] [9 "IX"] [5 "V"] [4 "IV"] [1 "I"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment