Merge pull request #90 from rebelwarrior/roman-numerals

adds roman-numerals exercise
This commit is contained in:
Erik Simmler 2016-07-18 12:36:42 -04:00 committed by GitHub
commit 9bc681b7c6
8 changed files with 158 additions and 2 deletions

View file

@ -30,7 +30,8 @@
"list-ops",
"atbash-cipher",
"say",
"largest-series-product"
"largest-series-product",
"roman-numerals"
],
"deprecated": [

View file

@ -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": {

View file

@ -0,0 +1 @@
module RomanNumerals exposing (toRoman)

View file

@ -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")
]

View file

@ -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

View file

@ -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"
}

View file

@ -0,0 +1,4 @@
@echo off
for %%f in (*Tests.elm) do (
elm-make %%f --yes --output build.js && node build.js
)

View file

@ -0,0 +1,2 @@
#!/usr/bin/env bash
elm-make *Tests.elm --yes --output build.js && node build.js