diff --git a/config.json b/config.json index 65d7fe5..4353965 100644 --- a/config.json +++ b/config.json @@ -30,7 +30,8 @@ "list-ops", "atbash-cipher", "say", - "largest-series-product" + "largest-series-product", + "roman-numerals" ], "deprecated": [ diff --git a/elm-package.json b/elm-package.json index 6f943a1..f8b1d08 100644 --- a/elm-package.json +++ b/elm-package.json @@ -31,7 +31,8 @@ "./exercises/list-ops", "./exercises/atbash-cipher", "./exercises/say", - "./exercises/largest-series-product" + "./exercises/largest-series-product", + "./exercises/roman-numerals" ], "exposed-modules": [], "dependencies": { diff --git a/exercises/roman-numerals/RomanNumerals.elm b/exercises/roman-numerals/RomanNumerals.elm new file mode 100644 index 0000000..2d26068 --- /dev/null +++ b/exercises/roman-numerals/RomanNumerals.elm @@ -0,0 +1 @@ +module RomanNumerals exposing (toRoman) diff --git a/exercises/roman-numerals/RomanNumerals.example b/exercises/roman-numerals/RomanNumerals.example new file mode 100644 index 0000000..4c42efb --- /dev/null +++ b/exercises/roman-numerals/RomanNumerals.example @@ -0,0 +1,46 @@ +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") + ] diff --git a/exercises/roman-numerals/RomanNumeralsTests.elm b/exercises/roman-numerals/RomanNumeralsTests.elm new file mode 100644 index 0000000..ed68abc --- /dev/null +++ b/exercises/roman-numerals/RomanNumeralsTests.elm @@ -0,0 +1,86 @@ +module Main exposing (..) + +import ElmTest exposing (..) +import RomanNumerals exposing (toRoman) + + +tests : Test +tests = + suite "Roman Numerals" + [ test "1" + (assertEqual ("I") + (toRoman 1) + ) + , test "2" + (assertEqual ("II") + (toRoman 2) + ) + , test "3" + (assertEqual ("III") + (toRoman 3) + ) + , test "4" + (assertEqual ("IV") + (toRoman 4) + ) + , test "5" + (assertEqual ("V") + (toRoman 5) + ) + , test "6" + (assertEqual ("VI") + (toRoman 6) + ) + , test "9" + (assertEqual ("IX") + (toRoman 9) + ) + , test "27" + (assertEqual ("XXVII") + (toRoman 27) + ) + , test "48" + (assertEqual ("XLVIII") + (toRoman 48) + ) + , test "59" + (assertEqual ("LIX") + (toRoman 59) + ) + , test "93" + (assertEqual ("XCIII") + (toRoman 93) + ) + , test "141" + (assertEqual ("CXLI") + (toRoman 141) + ) + , test "163" + (assertEqual ("CLXIII") + (toRoman 163) + ) + , test "402" + (assertEqual ("CDII") + (toRoman 402) + ) + , test "575" + (assertEqual ("DLXXV") + (toRoman 575) + ) + , test "911" + (assertEqual ("CMXI") + (toRoman 911) + ) + , test "1024" + (assertEqual ("MXXIV") + (toRoman 1024) + ) + , test "3000" + (assertEqual ("MMM") + (toRoman 3000) + ) + ] + +main : Program Never +main = + runSuite tests diff --git a/exercises/roman-numerals/elm-package.json b/exercises/roman-numerals/elm-package.json new file mode 100644 index 0000000..1ab2bdf --- /dev/null +++ b/exercises/roman-numerals/elm-package.json @@ -0,0 +1,15 @@ +{ + "version": "2.0.0", + "summary": "Exercism problems in Elm.", + "repository": "https://github.com/exercism/xelm.git", + "license": "BSD3", + "source-directories": [ + "." + ], + "exposed-modules": [], + "dependencies": { + "elm-community/elm-test": "1.0.0 <= v < 2.0.0", + "elm-lang/core": "4.0.0 <= v < 5.0.0" + }, + "elm-version": "0.17.0 <= v < 0.18.0" +} diff --git a/exercises/roman-numerals/runtests.bat b/exercises/roman-numerals/runtests.bat new file mode 100644 index 0000000..2a38cd8 --- /dev/null +++ b/exercises/roman-numerals/runtests.bat @@ -0,0 +1,4 @@ +@echo off +for %%f in (*Tests.elm) do ( + elm-make %%f --yes --output build.js && node build.js +) diff --git a/exercises/roman-numerals/runtests.sh b/exercises/roman-numerals/runtests.sh new file mode 100755 index 0000000..2bed7b2 --- /dev/null +++ b/exercises/roman-numerals/runtests.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +elm-make *Tests.elm --yes --output build.js && node build.js