Solving the Roman Numeral Kata in Clojure
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
(ns roman-numerals.core-test | |
(:require [clojure.test :refer :all])) | |
(def mapping { | |
0 "" | |
1 "I" | |
4 "IV" | |
5 "V" | |
9 "IX" | |
10 "X" | |
}) | |
(defn highest-reducer [x] | |
(let [x (->> mapping | |
keys | |
(filter #(<= % x)) | |
(apply max))] | |
[x (mapping x)])) | |
(defn convert | |
([x] (convert x "")) | |
([x result-string] | |
(let [[arabic roman] (highest-reducer x)] | |
(if (= x 0) | |
result-string | |
(recur (- x arabic) | |
(str result-string roman)))))) | |
(deftest convert-test | |
(testing "converts arabic numbers to roman numerals" | |
(are [x y] (= (convert x) y) | |
1 "I" | |
2 "II" | |
4 "IV" | |
5 "V" | |
6 "VI" | |
7 "VII" | |
8 "VIII" | |
9 "IX" | |
10 "X" | |
11 "XI" | |
13 "XIII" | |
14 "XIV" | |
))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment