Avatar of HegelPro

HegelPro's solution

to Roman Numerals in the Elm Track

Published at Jun 14 2019 · 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

See also: http://www.novaroma.org/via_romana/numbers.html

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

$ elm-test

Automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the test suite, be sure to remove the skip <| calls from each test until you get them all passing!

Source

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

Submitting Incomplete Solutions

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

Tests.elm

module Tests exposing (tests)

import Expect
import RomanNumerals exposing (toRoman)
import Test exposing (..)


tests : Test
tests =
    describe "Roman Numerals"
        [ test "1" <|
            \() ->
                Expect.equal "I"
                    (toRoman 1)
        , skip <|
            test "2" <|
                \() ->
                    Expect.equal "II"
                        (toRoman 2)
        , skip <|
            test "3" <|
                \() ->
                    Expect.equal "III"
                        (toRoman 3)
        , skip <|
            test "4" <|
                \() ->
                    Expect.equal "IV"
                        (toRoman 4)
        , skip <|
            test "5" <|
                \() ->
                    Expect.equal "V"
                        (toRoman 5)
        , skip <|
            test "6" <|
                \() ->
                    Expect.equal "VI"
                        (toRoman 6)
        , skip <|
            test "9" <|
                \() ->
                    Expect.equal "IX"
                        (toRoman 9)
        , skip <|
            test "27" <|
                \() ->
                    Expect.equal "XXVII"
                        (toRoman 27)
        , skip <|
            test "48" <|
                \() ->
                    Expect.equal "XLVIII"
                        (toRoman 48)
        , skip <|
            test "59" <|
                \() ->
                    Expect.equal "LIX"
                        (toRoman 59)
        , skip <|
            test "93" <|
                \() ->
                    Expect.equal "XCIII"
                        (toRoman 93)
        , skip <|
            test "141" <|
                \() ->
                    Expect.equal "CXLI"
                        (toRoman 141)
        , skip <|
            test "163" <|
                \() ->
                    Expect.equal "CLXIII"
                        (toRoman 163)
        , skip <|
            test "402" <|
                \() ->
                    Expect.equal "CDII"
                        (toRoman 402)
        , skip <|
            test "575" <|
                \() ->
                    Expect.equal "DLXXV"
                        (toRoman 575)
        , skip <|
            test "911" <|
                \() ->
                    Expect.equal "CMXI"
                        (toRoman 911)
        , skip <|
            test "1024" <|
                \() ->
                    Expect.equal "MXXIV"
                        (toRoman 1024)
        , skip <|
            test "3000" <|
                \() ->
                    Expect.equal "MMM"
                        (toRoman 3000)
        ]
module RomanNumerals exposing (toRoman)

import Array exposing (..)

toRoman : Int -> String
toRoman number =
    number
        |> String.fromInt
        |> String.toList
        |> List.map (\n -> (Maybe.withDefault 0 << String.toInt << String.fromChar) n)
        |> List.reverse
        |> Array.fromList
        |> formatArrayToString

normalDigitToRomanDicit : String -> String -> String -> Int -> String
normalDigitToRomanDicit charOne charTwo charThree number =
    if number == 0 then
        ""
    else if number <= 3 then
        String.repeat number charOne
    else if number == 4 then
        charOne ++ charTwo
    else if number == 5 then
        charTwo
    else if number <= 8 then
        charTwo ++ String.repeat (number - 5) charOne
    else if number == 9 then
        charOne ++ charThree
    else
        "Error"

formatArrayToString array =
    let
        firstChar = (normalDigitToRomanDicit "I" "V" "X" << Maybe.withDefault 0 << Array.get 0) array
        secondChar = (normalDigitToRomanDicit "X" "L" "C" << Maybe.withDefault 0 << Array.get 1) array
        thirdChar = (normalDigitToRomanDicit "C" "D" "M" << Maybe.withDefault 0 << Array.get 2) array
        fourthChar = (normalDigitToRomanDicit "M" "" "" << Maybe.withDefault 0 << Array.get 3) array
    in
        fourthChar ++ thirdChar ++ secondChar ++ firstChar

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?