Created
August 4, 2012 17:04
-
-
Save ulsa/3258784 to your computer and use it in GitHub Desktop.
Decimal to Roman Numeral converter
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
(let [lst (filter odd? [1 2 3 4 5]) | |
fst (first lst) | |
snd (second lst)] | |
(println fst snd)) | |
=> 1 3 |
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
(filter #(>= 6 (first %)) mapping) | |
=> ([5 "V"] [4 "IV"] [1 "I"]) | |
(filter #(>= 4 (first %)) mapping) | |
=> ([4 "IV"] [1 "I"]) |
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
(filter odd? [1 2 3 4 5]) | |
=> (1 3 5) |
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
(fn [x] | |
(>= i (first x))) |
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
#(>= i (first %)) |
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
{10 "X" 9 "IX" 5 "V" 4 "IV" 1 "I"} | |
=> {1 "I", 10 "X", 4 "IV", 5 "V", 9 "IX"} |
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
(sorted-map 10 "X" 9 "IX" 5 "V" 4 "IV" 1 "I") | |
=> {1 "I", 4 "IV", 5 "V", 9 "IX", 10 "X"} |
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
(sorted-map-by > 10 "X" 9 "IX" 5 "V" 4 "IV" 1 "I") | |
=> {10 "X", 9 "IX", 5 "V", 4 "IV", 1 "I"} |
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 mapping (sorted-map-by > 1000 "M" 900 "CM" 500 "D" 400 "CD" | |
100 "C" 90 "XC" 50 "L" 40 "XL" | |
10 "X" 9 "IX" 5 "V" 4 "IV" 1 "I")) | |
(defn roman-numeral [i] | |
(loop [i i s ""] | |
(if (zero? i) | |
s | |
(let [[[n c]] (filter #(>= i (first %)) mapping)] | |
(recur (- i n) (str s c)))))) |
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
(seq (sorted-map-by > 10 "X" 9 "IX" 5 "V" 4 "IV" 1 "I")) | |
=> ([10 "X"] [9 "IX"] [5 "V"] [4 "IV"] [1 "I"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment