Last active
August 29, 2015 14:04
-
-
Save gfrivolt/6531e607e9d03bdea849 to your computer and use it in GitHub Desktop.
Budapest Clojure Meetup Coding Dojo
This file contains hidden or 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
(ns roman-nums.core | |
(:require | |
[clojure.test :refer [is testing]])) | |
(def roman-digits [[1000 "M"] | |
[500 "D"] | |
[400 "CD"] | |
[100 "C"] | |
[50 "L"] | |
[40 "XL"] | |
[10 "X"] | |
[5 "V"] | |
[4 "IV"] | |
[1 "I"]]) | |
(defn num-to-roman | |
"Convert a number to roman" | |
[n] | |
(if (pos? n) | |
(let [[value romnum] (first (filter #(>= n (first %)) roman-digits))] | |
(str romnum (num-to-roman (- n value)))) | |
"" | |
) | |
) | |
(testing "roman for 0" | |
(is (= (num-to-roman 0) "")) | |
) | |
(testing "roman for 1" | |
(is (= (num-to-roman 1) "I")) | |
) | |
(testing "roman for 3" | |
(is (= (num-to-roman 3) "III")) | |
) | |
(testing "roman for 4" | |
(is (= (num-to-roman 4) "IV")) | |
) | |
(testing "roman for 41" | |
(is (= (num-to-roman 41) "XLI")) | |
) | |
(testing "roman for 971" | |
(is (= (num-to-roman 971) "DCDLXXI")) | |
) |
Yes should be CMLXXI, as for 91 is XCI and 9 is IX.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
your last example, 971 seems wrong to me.