Write a function to convert from normal numbers to Roman Numerals.
The Romans were a clever bunch. They conquered most of Europe and ruled it for hundreds of years. They invented concrete and straight roads and even bikinis. One thing they never discovered though was the number zero. This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. For example the BBC uses Roman numerals to date their programmes.
The Romans wrote numbers using letters - I, V, X, L, C, D, M. (notice these letters have lots of straight lines and are hence easy to hack into stone tablets).
1 => I
10 => X
7 => VII
There is no need to be able to convert numbers larger than about 3000. (The Romans themselves didn't tend to go any higher)
Wikipedia says: Modern Roman numerals ... are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero.
To see this in practice, consider the example of 1990.
In Roman numerals 1990 is MCMXC:
1000=M 900=CM 90=XC
2008 is written as MMVIII:
2000=MM 8=VIII
See also: http://www.novaroma.org/via_romana/numbers.html
For library documentation, follow Useful OCaml resources.
A Makefile
is provided with a default target to compile your solution and run the tests. At the command line, type:
make
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.
If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue or submit a PR. We welcome new contributors!
The Roman Numeral Kata http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals
open OUnit2
open Roman_numerals
let ae expected actual _ctx = assert_equal ~printer:(fun x -> x) expected actual
let tests = [
"1 is a single I" >::
ae "I" (to_roman 1);
"2 is two I's" >::
ae "II" (to_roman 2);
"3 is three I's" >::
ae "III" (to_roman 3);
"4, being 5 - 1, is IV" >::
ae "IV" (to_roman 4);
"5 is a single V" >::
ae "V" (to_roman 5);
"6, being 5 + 1, is VI" >::
ae "VI" (to_roman 6);
"9, being 10 - 1, is IX" >::
ae "IX" (to_roman 9);
"20 is two X's" >::
ae "XXVII" (to_roman 27);
"48 is not 50 - 2 but rather 40 + 8" >::
ae "XLVIII" (to_roman 48);
"49 is not 40 + 5 + 4 but rather 50 - 10 + 10 - 1" >::
ae "XLIX" (to_roman 49);
"50 is a single L" >::
ae "LIX" (to_roman 59);
"90, being 100 - 10, is XC" >::
ae "XCIII" (to_roman 93);
"100 is a single C" >::
ae "CXLI" (to_roman 141);
"60, being 50 + 10, is LX" >::
ae "CLXIII" (to_roman 163);
"400, being 500 - 100, is CD" >::
ae "CDII" (to_roman 402);
"500 is a single D" >::
ae "DLXXV" (to_roman 575);
"900, being 1000 - 100, is CM" >::
ae "CMXI" (to_roman 911);
"1000 is a single M" >::
ae "MXXIV" (to_roman 1024);
"3000 is three M's" >::
ae "MMM" (to_roman 3000);
]
let () =
run_test_tt_main ("roman-numerals test" >::: tests)
open Base
let pairs =
[ ("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) ]
let repeat str n = List.init n ~f:(Fn.const str) |> String.concat
let to_roman n =
let rec aux acc n = function
| [] ->
List.rev acc
| (symbol, value) :: tl ->
aux (repeat symbol (n / value) :: acc) (n % value) tl
in
aux [] n pairs |> String.concat
(* Roman numerals exercise *)
(** Convert an integer value to its Roman Numeral representation up to 3000 inclusive *)
val to_roman : int -> string
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,089 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments