Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@celwell
Created February 8, 2020 21:23
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 celwell/43f6a3c38087287da3d5312320460fb0 to your computer and use it in GitHub Desktop.
Save celwell/43f6a3c38087287da3d5312320460fb0 to your computer and use it in GitHub Desktop.
Solution to 4Clojure #92 - Read Roman Numerals
(fn [roman-str]
(let [letter-val {\0 0 ;; for padding last tuple
\I 1, \V 5, \X 10, \L 50
\C 100, \D 500, \M 1000}
letter-tuple->dec (fn [[l r]]
(* (letter-val l)
(if (< (letter-val l) (letter-val r))
-1
1)))]
(->> roman-str
(partition 2 1 [\0])
(map letter-tuple->dec)
(reduce + 0))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment