2016-07-18 14:17:37 +00:00
|
|
|
module RomanNumerals exposing (toRoman)
|
|
|
|
|
|
|
|
import Dict
|
|
|
|
import Maybe
|
|
|
|
|
|
|
|
|
|
|
|
toRoman : Int -> String
|
|
|
|
toRoman number =
|
2016-08-17 11:14:17 +00:00
|
|
|
if number == 0 then
|
|
|
|
""
|
|
|
|
else
|
|
|
|
let
|
|
|
|
part =
|
|
|
|
largestFactor number
|
|
|
|
|
|
|
|
letter =
|
|
|
|
numerals
|
|
|
|
|> Dict.get part
|
|
|
|
|> Maybe.withDefault ""
|
|
|
|
in
|
|
|
|
letter ++ (toRoman (number - part))
|
|
|
|
|
2016-07-18 14:17:37 +00:00
|
|
|
|
|
|
|
largestFactor : Int -> Int
|
|
|
|
largestFactor number =
|
2016-08-17 11:14:17 +00:00
|
|
|
numerals
|
|
|
|
|> Dict.keys
|
|
|
|
|> List.filter (\p -> p <= number)
|
|
|
|
|> List.reverse
|
|
|
|
|> List.head
|
|
|
|
|> Maybe.withDefault 0
|
2016-07-18 14:17:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
numerals : Dict.Dict Int String
|
2016-08-17 11:14:17 +00:00
|
|
|
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" )
|
|
|
|
]
|