# 4d47's solution

## to Roman Numerals in the Erlang Track

Published at Jul 27 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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

## Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3` is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

``````\$ rebar3 eunit
``````

### Test versioning

Each problem defines a macro `TEST_VERSION` in the test file and verifies that the solution defines and exports a function `test_version` returning that same value.

``````-export([test_version/0]).

test_version() ->
1.
``````

The benefit of this is that reviewers can see against which test version an iteration was written if, for example, a previously posted solution does not solve the current problem or passes current tests.

## Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Source

The Roman Numeral Kata http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### roman_numerals_tests.erl

``````-module(roman_numerals_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

expect_roman(Number, Expected) ->
?assertEqual(Expected, roman_numerals:numerals(Number)).

convert_1_test() -> expect_roman(1, "I").

convert_2_test() -> expect_roman(2, "II").

convert_3_test() -> expect_roman(3, "III").

convert_4_test() -> expect_roman(4, "IV").

convert_5_test() -> expect_roman(5, "V").

convert_6_test() -> expect_roman(6, "VI").

convert_9_test() -> expect_roman(9, "IX").

convert_27_test() -> expect_roman(27, "XXVII").

convert_48_test() -> expect_roman(48, "XLVIII").

convert_59_test() -> expect_roman(59, "LIX").

convert_93_test() -> expect_roman(93, "XCIII").

convert_141_test() -> expect_roman(141, "CXLI").

convert_163_test() -> expect_roman(163, "CLXIII").

convert_402_test() -> expect_roman(402, "CDII").

convert_575_test() -> expect_roman(575, "DLXXV").

convert_911_test() -> expect_roman(911, "CMXI").

convert_1024_test() -> expect_roman(1024, "MXXIV").

convert_3000_test() -> expect_roman(3000, "MMM").

version_test() ->
?assertMatch(1, roman_numerals:test_version()).``````
``````-module(roman_numerals).
-export([numerals/1, test_version/0]).

test_version() ->
1.

numerals(N) ->
Nums = [
{1000, "M"},
{ 900, "CM"},
{ 500, "D"},
{ 400, "CD"},
{ 100, "C"},
{  90, "XC"},
{  50, "L"},
{  40, "XL"},
{  10, "X"},
{   9, "IX"},
{   5, "V"},
{   4, "IV"},
{   1, "I"}
],
{0, Result} = lists:foldl(fun romancat/2, {N, ""}, Nums),
Result.

romancat({Dec, Letters}, {N, Result}) ->
{N rem Dec, lists:concat([Result, string:copies(Letters, N div Dec)])}.``````