Skip to content

Instantly share code, notes, and snippets.

Created March 1, 2012 05:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1947594 to your computer and use it in GitHub Desktop.
Save anonymous/1947594 to your computer and use it in GitHub Desktop.
;; ariarule's solution to Write Roman Numerals
;; https://4clojure.com/problem/104
(fn to-roman [n]
(let [sep-rom (take-last 4 (map #(Integer/parseInt (str %)) (str "000" n)))
rom-m (first sep-rom)
rom-less (rest sep-rom)]
(apply str
(concat (repeat rom-m \M)
(mapcat
#(cond
(<= %1 3) (repeat %1 %2)
(= %1 4) (list %2 (:v %3))
(<= %1 8) (concat (list (:v %3)) (repeat (- %1 5) %2))
:9 (list %2 (:x %3)))
rom-less
'(\C \X \I)
'({:v \D :x \M}
{:v \L :x \C}
{:v \V :x \X}))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment