Sha256: 91a46f664395ae5038b0168753a5006be1a1ceb851baec865c68bb4eee5232ea

Contents?: true

Size: 974 Bytes

Versions: 74

Compression:

Stored size: 974 Bytes

Contents

module RomanNumerals exposing (toRoman)

import Dict
import Maybe


toRoman : Int -> String
toRoman number =
    if number == 0 then
        ""
    else
        let
            part =
                largestFactor number

            letter =
                numerals
                    |> Dict.get part
                    |> Maybe.withDefault ""
        in
        letter ++ toRoman (number - part)


largestFactor : Int -> Int
largestFactor number =
    numerals
        |> Dict.keys
        |> List.filter (\p -> p <= number)
        |> List.reverse
        |> List.head
        |> Maybe.withDefault 0


numerals : Dict.Dict Int String
numerals =
    Dict.fromList
        [ ( 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" )
        ]

Version data entries

74 entries across 74 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.179 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.178 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.177 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.176 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.175 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.174 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.173 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.172 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.171 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.170 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.169 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.167 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.166 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.165 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.164 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.163 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.162 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.161 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm
trackler-2.2.1.160 tracks/elm/exercises/roman-numerals/RomanNumerals.example.elm