exercism

Exercism solutions.
git clone git://code.dwrz.net/exercism
Log | Files | Refs

roman-numerals.el (1083B)


      1 ;;; roman-numerals.el --- roman-numerals Exercise (exercism)
      2 
      3 ;;; Commentary:
      4 
      5 ;;; Code:
      6 
      7 (provide 'roman-numerals)
      8 
      9 (defun greatest-roman-amount (n)
     10   (cond ((>= n 1000) 1000)
     11         ((>= n 900) 900)
     12         ((>= n 500) 500)
     13         ((>= n 400) 400)
     14         ((>= n 100) 100)
     15         ((>= n 90) 90)
     16         ((>= n 50) 50)
     17         ((>= n 40) 40)
     18         ((>= n 10) 10)
     19         ((>= n 9) 9)
     20         ((>= n 5) 5)
     21         ((>= n 4) 4)
     22         ((>= n 1) 1)))
     23 
     24 (defun numeral-for-amount (n)
     25   (cond ((= n 1000) "M")
     26         ((= n 900) "CM")
     27         ((= n 500) "D")
     28         ((= n 400) "CD")
     29         ((= n 100) "C")
     30         ((= n 90) "XC")
     31         ((= n 50) "L")
     32         ((= n 40) "XL")
     33         ((= n 10) "X")
     34         ((= n 9) "IX")
     35         ((= n 5) "V")
     36         ((= n 4) "IV")
     37         ((= n 1) "I")))
     38 
     39 (defun to-roman (n)
     40 "Convert whole number N to roman numeral.
     41 Does not handle numbers greater than 3000."
     42   (cl-loop until (<= n 0)
     43            concat (numeral-for-amount (greatest-roman-amount n))
     44            do (setq n (- n (greatest-roman-amount n)))))
     45 
     46 ;;; roman-numerals.el ends here