ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# lujanfernaud's solution

## to Roman Numerals in the Elixir Track

Published at May 02 2020 · 0 comments
Instructions
Test suite
Solution

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

Execute the tests with:

``````\$ mix test
``````

### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.

For example:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

## 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_test.exs

``````defmodule RomanNumeralsTest do
use ExUnit.Case

# @tag :pending
test "1" do
assert RomanNumerals.numeral(1) == "I"
end

@tag :pending
test "2" do
assert RomanNumerals.numeral(2) == "II"
end

@tag :pending
test "3" do
assert RomanNumerals.numeral(3) == "III"
end

@tag :pending
test "4" do
assert RomanNumerals.numeral(4) == "IV"
end

@tag :pending
test "5" do
assert RomanNumerals.numeral(5) == "V"
end

@tag :pending
test "6" do
assert RomanNumerals.numeral(6) == "VI"
end

@tag :pending
test "9" do
assert RomanNumerals.numeral(9) == "IX"
end

@tag :pending
test "27" do
assert RomanNumerals.numeral(27) == "XXVII"
end

@tag :pending
test "48" do
assert RomanNumerals.numeral(48) == "XLVIII"
end

@tag :pending
test "59" do
assert RomanNumerals.numeral(59) == "LIX"
end

@tag :pending
test "93" do
assert RomanNumerals.numeral(93) == "XCIII"
end

@tag :pending
test "141" do
assert RomanNumerals.numeral(141) == "CXLI"
end

@tag :pending
test "163" do
assert RomanNumerals.numeral(163) == "CLXIII"
end

@tag :pending
test "402" do
assert RomanNumerals.numeral(402) == "CDII"
end

@tag :pending
test "575" do
assert RomanNumerals.numeral(575) == "DLXXV"
end

@tag :pending
test "911" do
assert RomanNumerals.numeral(911) == "CMXI"
end

@tag :pending
test "1024" do
assert RomanNumerals.numeral(1024) == "MXXIV"
end

@tag :pending
test "3000" do
assert RomanNumerals.numeral(3000) == "MMM"
end
end``````

### test_helper.exs

``````ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)``````
``````defmodule RomanNumerals do
@doc """
Convert the number to a roman number.
"""
@spec numeral(pos_integer) :: String.t()
def numeral(number) do
number
|> convert_to_list()
|> prepare_decimals()
|> convert_to_roman_numeral()
end

defp convert_to_list(number) do
number
|> Integer.to_string()
|> String.graphemes()
|> Enum.map(&String.to_integer/1)
end

defp prepare_decimals(number_list) do
number_list
|> Enum.reverse()
|> Enum.zip([1, 10, 100, 1000])
|> Enum.map(fn {number, multiplier} -> number * multiplier end)
|> Enum.reverse()
end

defp convert_to_roman_numeral(number_list) do
number_list
|> Enum.map(&to_roman/1)
|> Enum.join()
end

defp to_roman(0), do: ""
defp to_roman(1), do: "I"
defp to_roman(2), do: "II"
defp to_roman(3), do: "III"
defp to_roman(4), do: "IV"
defp to_roman(5), do: "V"
defp to_roman(6), do: "VI"
defp to_roman(7), do: "VII"
defp to_roman(8), do: "VIII"
defp to_roman(9), do: "IX"
defp to_roman(10), do: "X"
defp to_roman(20), do: "XX"
defp to_roman(30), do: "XXX"
defp to_roman(40), do: "XL"
defp to_roman(50), do: "L"
defp to_roman(60), do: "LX"
defp to_roman(70), do: "LXX"
defp to_roman(80), do: "LXXX"
defp to_roman(90), do: "XC"
defp to_roman(100), do: "C"
defp to_roman(200), do: "CC"
defp to_roman(300), do: "CCC"
defp to_roman(400), do: "CD"
defp to_roman(500), do: "D"
defp to_roman(600), do: "DC"
defp to_roman(700), do: "DCC"
defp to_roman(800), do: "DCCC"
defp to_roman(900), do: "CM"
defp to_roman(1000), do: "M"
defp to_roman(2000), do: "MM"
defp to_roman(3000), do: "MMM"
end``````
The text passage contains a description of a coding challenge to convert numbers to Roman numerals, along with a solution in the Elixir programming language and a test suite for the solution.
no_question

the passage does not contain a mathematical question or a request for a calculation. it is a description of a programming task and its solution.
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# lujanfernaud's solution

## to Roman Numerals in the Elixir Track

Published at May 02 2020 · 0 comments
Instructions
Test suite
Solution

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

Execute the tests with:

``````\$ mix test
``````

### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.

For example:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

## 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_test.exs

``````defmodule RomanNumeralsTest do
use ExUnit.Case

# @tag :pending
test "1" do
assert RomanNumerals.numeral(1) == "I"
end

@tag :pending
test "2" do
assert RomanNumerals.numeral(2) == "II"
end

@tag :pending
test "3" do
assert RomanNumerals.numeral(3) == "III"
end

@tag :pending
test "4" do
assert RomanNumerals.numeral(4) == "IV"
end

@tag :pending
test "5" do
assert RomanNumerals.numeral(5) == "V"
end

@tag :pending
test "6" do
assert RomanNumerals.numeral(6) == "VI"
end

@tag :pending
test "9" do
assert RomanNumerals.numeral(9) == "IX"
end

@tag :pending
test "27" do
assert RomanNumerals.numeral(27) == "XXVII"
end

@tag :pending
test "48" do
assert RomanNumerals.numeral(48) == "XLVIII"
end

@tag :pending
test "59" do
assert RomanNumerals.numeral(59) == "LIX"
end

@tag :pending
test "93" do
assert RomanNumerals.numeral(93) == "XCIII"
end

@tag :pending
test "141" do
assert RomanNumerals.numeral(141) == "CXLI"
end

@tag :pending
test "163" do
assert RomanNumerals.numeral(163) == "CLXIII"
end

@tag :pending
test "402" do
assert RomanNumerals.numeral(402) == "CDII"
end

@tag :pending
test "575" do
assert RomanNumerals.numeral(575) == "DLXXV"
end

@tag :pending
test "911" do
assert RomanNumerals.numeral(911) == "CMXI"
end

@tag :pending
test "1024" do
assert RomanNumerals.numeral(1024) == "MXXIV"
end

@tag :pending
test "3000" do
assert RomanNumerals.numeral(3000) == "MMM"
end
end``````

### test_helper.exs

``````ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)``````
``````defmodule RomanNumerals do
@doc """
Convert the number to a roman number.
"""
@spec numeral(pos_integer) :: String.t()
def numeral(number) do
number
|> convert_to_list()
|> prepare_decimals()
|> convert_to_roman_numeral()
end

defp convert_to_list(number) do
number
|> Integer.to_string()
|> String.graphemes()
|> Enum.map(&String.to_integer/1)
end

defp prepare_decimals(number_list) do
number_list
|> Enum.reverse()
|> Enum.zip([1, 10, 100, 1000])
|> Enum.map(fn {number, multiplier} -> number * multiplier end)
|> Enum.reverse()
end

defp convert_to_roman_numeral(number_list) do
number_list
|> Enum.map(&to_roman/1)
|> Enum.join()
end

defp to_roman(0), do: ""
defp to_roman(1), do: "I"
defp to_roman(2), do: "II"
defp to_roman(3), do: "III"
defp to_roman(4), do: "IV"
defp to_roman(5), do: "V"
defp to_roman(6), do: "VI"
defp to_roman(7), do: "VII"
defp to_roman(8), do: "VIII"
defp to_roman(9), do: "IX"
defp to_roman(10), do: "X"
defp to_roman(20), do: "XX"
defp to_roman(30), do: "XXX"
defp to_roman(40), do: "XL"
defp to_roman(50), do: "L"
defp to_roman(60), do: "LX"
defp to_roman(70), do: "LXX"
defp to_roman(80), do: "LXXX"
defp to_roman(90), do: "XC"
defp to_roman(100), do: "C"
defp to_roman(200), do: "CC"
defp to_roman(300), do: "CCC"
defp to_roman(400), do: "CD"
defp to_roman(500), do: "D"
defp to_roman(600), do: "DC"
defp to_roman(700), do: "DCC"
defp to_roman(800), do: "DCCC"
defp to_roman(900), do: "CM"
defp to_roman(1000), do: "M"
defp to_roman(2000), do: "MM"
defp to_roman(3000), do: "MMM"
end``````
user
The text passage contains a description of a coding challenge to convert numbers to Roman numerals, along with a solution in the Elixir programming language and a test suite for the solution.

TYPE: NO_QUESTION

The passage does not contain a mathematical question or a request for a calculation. It is a description of a programming task and its solution.