Skip to content

Added roman-numerals exercise. #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"robot-name",
"rna-transcription",
"atbash-cipher",
"raindrops"
"raindrops",
"roman-numerals"
],
"deprecated": [

Expand Down
36 changes: 36 additions & 0 deletions exercises/roman-numerals/example.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
;;; roman-numerals.el --- roman-numerals Exercise (exercism)

;;; Commentary:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I prefer when over single branch if.

;;; Code:


(defun to-roman (value)
(let ((decode '(("M" . 1000)
("CM" . 900)
("D" . 500)
("CD" . 400)
("C" . 100)
("XC" . 90)
("L" . 50)
("XL" . 40)
("X" . 10)
("IX" . 9)
("V" . 5)
("IV" . 4)
("I" . 1)))
(roman nil))
(if (> value 3000)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 3000? You can make Roman numerals up through 4999.

(error "Value out of range"))
(while (not (eq value 0))
(let ((r (caar decode))
(d (cdar decode)))
(when (>= value d)
(setq roman (append roman (make-list (/ value d) r)))
(setq value (% value d)))
(setq decode (cdr decode))))
(apply 'concat roman)))


(provide 'roman-numerals)
;;; roman-numerals.el ends here
30 changes: 30 additions & 0 deletions exercises/roman-numerals/roman-numerals-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
;;; roman-numerals-test.el --- Tests for roman-numerals (exercism)

;;; Commentary:

;;; Code:

(load-file "roman-numerals.el")

(ert-deftest roman-numerals-test ()
(should (equal (to-roman 1) "I"))
(should (equal (to-roman 2) "II"))
(should (equal (to-roman 3) "III"))
(should (equal (to-roman 4) "IV"))
(should (equal (to-roman 5) "V"))
(should (equal (to-roman 6) "VI"))
(should (equal (to-roman 9) "IX"))
(should (equal (to-roman 27) "XXVII"))
(should (equal (to-roman 48) "XLVIII"))
(should (equal (to-roman 59) "LIX"))
(should (equal (to-roman 93) "XCIII"))
(should (equal (to-roman 141) "CXLI"))
(should (equal (to-roman 163) "CLXIII"))
(should (equal (to-roman 402) "CDII"))
(should (equal (to-roman 575) "DLXXV"))
(should (equal (to-roman 911) "CMXI"))
(should (equal (to-roman 1024) "MXXIV"))
(should (equal (to-roman 3000) "MMM")))

(provide 'roman-numerals)
;;; roman-numerals-test.el ends here
10 changes: 10 additions & 0 deletions exercises/roman-numerals/roman-numerals.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
;;; roman-numerals.el --- roman-numerals Exercise (exercism)

;;; Commentary:

;;; Code:



(provide 'roman-numerals)
;;; roman-numerals.el ends here