Skip to content

Instantly share code, notes, and snippets.

@adomokos
Created May 15, 2015 18:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adomokos/5e16570fc650d4709e1a to your computer and use it in GitHub Desktop.
Save adomokos/5e16570fc650d4709e1a to your computer and use it in GitHub Desktop.
Solving the Roman Numeral Kata in Clojure
(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