-
-
Save cellularmitosis/567d7fb89b35e1e8cf7743ad5a278b4e to your computer and use it in GitHub Desktop.
This file contains 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
(def table [ | |
["M" 1000] | |
["D" 500] | |
["CD" 400] | |
["C" 100] | |
["L" 50] | |
["XL" 40] | |
["X" 10] | |
["IX" 9] | |
["V" 5] | |
["IV" 4] | |
["I" 1]]) | |
(defn roman [i table] | |
(cond | |
(= i 0) "" | |
(= table '()) "" | |
:else | |
(let [ | |
symbl (first (first table)) | |
value (last (first table)) | |
quotient (unchecked-divide-int i value) | |
roman-head-str (apply str (repeat quotient symbl)) | |
roman-tail-str (roman (- i (* quotient value)) (rest table))] | |
(apply str roman-head-str roman-tail-str)))) | |
(defn test-roman [i] | |
(printf "roman(%s): %s\n" i (roman i table))) | |
(run! test-roman [1 3 4 5 6 8 9 10 11 14 15 16 39 40 49 50 99 100]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment