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:
See also: http://www.novaroma.org/via_romana/numbers.html
For installation and learning resources, refer to the exercism help page.
To work on the exercises, you will need
Base. Consult opam website for instructions on how to install
opam for your OS. Once
opam is installed open a terminal window and run the following command to install base:
opam install base
To run the tests you will need
OUnit. Install it using
opam install ounit
A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:
utop is a command line program which allows you to run Ocaml code interactively. The easiest way to install it is via opam:
opam install utop
Consult utop for more detail.
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. We'll do our best to help you!
The Roman Numeral Kata http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
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 replicate n x = let rec rep xs = function | 0 -> xs | n -> rep (x :: xs) (n - 1) in rep  n let to_roman n = let digit x = n / (10 ** (x - 1)) % 10 in let roman x one five ten = match digit x with | num when num <= 3 -> replicate num one | num when num = 4 -> [one; five] | num when num <= 8 -> five :: replicate (num - 5) one | num -> List.concat [replicate (10 - num) one; [ten]] in let r1 = roman 1 'I' 'V' 'X' in let r2 = roman 2 'X' 'L' 'C' in let r3 = roman 3 'C' 'D' 'M' in let r4 = replicate (digit 4) 'M' in String.of_char_list (List.concat [r4; r3; r2; r1])
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.