Skip to content

Instantly share code, notes, and snippets.

@saskali
Last active August 26, 2019 20:07
Show Gist options
  • Save saskali/f45c3636186f16cef6e409c3b2f48fe6 to your computer and use it in GitHub Desktop.
Save saskali/f45c3636186f16cef6e409c3b2f48fe6 to your computer and use it in GitHub Desktop.
Roman Numerals - kata @socrates 2019
(ns roman-numerals.core-test
(:require [clojure.test :refer :all]))
(def roman-digit->arabic
{\I 1
\V 5
\X 10
\L 50
\C 100})
;; problem: the accumulation is arabic, but negagtive are in the romain world
(defn arabic-compromiser [result list-of-nums]
(cond
(empty? list-of-nums) result
(= 1 (count list-of-nums)) (+ result (first list-of-nums))
:else (let [first-num (first list-of-nums)
second-num (second list-of-nums)
rest-list-nums (rest list-of-nums)]
(if (>= first-num second-num)
(recur (+ result first-num) rest-list-nums)
(recur (+ result (- second-num first-num)) (rest rest-list-nums))))))
(defn roman->arabic [roman]
(->> roman
(map roman-digit->arabic)
(arabic-compromiser 0)))
(deftest roman->arabic-should
(testing "tranlate roman digits to arabic"
(is (= 1 (roman->arabic "I")))
(is (= 5 (roman->arabic "V")))
(is (= 10 (roman->arabic "X")))
(is (= 50 (roman->arabic "L")))
(is (= 100 (roman->arabic "C"))))
(testing "add repeated digits that are equals"
(is (= 2 (roman->arabic "II")))
(is (= 3 (roman->arabic "III")))
(is (= 20 (roman->arabic "XX")))
(is (= 30 (roman->arabic "XXX")))
(is (= 200 (roman->arabic "CC")))
(is (= 300 (roman->arabic "CCC"))))
(testing "add repeated digits that are decreasing order"
(is (= 15 (roman->arabic "XV")))
(is (= 110 (roman->arabic "CX"))))
(testing "subtracts digits that are in increasing order"
(is (= 4 (roman->arabic "IV")))
(is (= 9 (roman->arabic "IX")))
(is (= 90 (roman->arabic "XC")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment