Skip to content

Instantly share code, notes, and snippets.

@adamdavislee
Created September 21, 2018 16:01
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 adamdavislee/6e051bbd252fdf01cd0289281013925f to your computer and use it in GitHub Desktop.
Save adamdavislee/6e051bbd252fdf01cd0289281013925f to your computer and use it in GitHub Desktop.
(with-test
(defn roman-numeral
[input]
(loop [input input, acc 0, ]
(if (empty? input)
acc
(let [convert {"I" 1, "V" 5, "X" 10, "L" 50, "C" 100, "D" 500, "M" 1000, }
curr-num (convert (subs input 0 1))
op (if (and (not= 1 (count input))
(< curr-num
(convert (subs input 1 2))))
- +)]
(recur (subs input 1)
(op acc curr-num))))))
(are
[a b] (= (roman-numeral a) b),
"XIV" 14,
"DCCCXXVII" 827,
"MMMCMXCIX" 3999,
"XLVIII" 48, ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment