mirror of
https://github.com/correl/elm.git
synced 2024-12-20 03:20:17 +00:00
Merge pull request #90 from rebelwarrior/roman-numerals
adds roman-numerals exercise
This commit is contained in:
commit
9bc681b7c6
8 changed files with 158 additions and 2 deletions
|
@ -30,7 +30,8 @@
|
||||||
"list-ops",
|
"list-ops",
|
||||||
"atbash-cipher",
|
"atbash-cipher",
|
||||||
"say",
|
"say",
|
||||||
"largest-series-product"
|
"largest-series-product",
|
||||||
|
"roman-numerals"
|
||||||
],
|
],
|
||||||
"deprecated": [
|
"deprecated": [
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,8 @@
|
||||||
"./exercises/list-ops",
|
"./exercises/list-ops",
|
||||||
"./exercises/atbash-cipher",
|
"./exercises/atbash-cipher",
|
||||||
"./exercises/say",
|
"./exercises/say",
|
||||||
"./exercises/largest-series-product"
|
"./exercises/largest-series-product",
|
||||||
|
"./exercises/roman-numerals"
|
||||||
],
|
],
|
||||||
"exposed-modules": [],
|
"exposed-modules": [],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
1
exercises/roman-numerals/RomanNumerals.elm
Normal file
1
exercises/roman-numerals/RomanNumerals.elm
Normal file
|
@ -0,0 +1 @@
|
||||||
|
module RomanNumerals exposing (toRoman)
|
46
exercises/roman-numerals/RomanNumerals.example
Normal file
46
exercises/roman-numerals/RomanNumerals.example
Normal 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")
|
||||||
|
]
|
86
exercises/roman-numerals/RomanNumeralsTests.elm
Normal file
86
exercises/roman-numerals/RomanNumeralsTests.elm
Normal 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
|
15
exercises/roman-numerals/elm-package.json
Normal file
15
exercises/roman-numerals/elm-package.json
Normal 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"
|
||||||
|
}
|
4
exercises/roman-numerals/runtests.bat
Normal file
4
exercises/roman-numerals/runtests.bat
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
@echo off
|
||||||
|
for %%f in (*Tests.elm) do (
|
||||||
|
elm-make %%f --yes --output build.js && node build.js
|
||||||
|
)
|
2
exercises/roman-numerals/runtests.sh
Executable file
2
exercises/roman-numerals/runtests.sh
Executable file
|
@ -0,0 +1,2 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
elm-make *Tests.elm --yes --output build.js && node build.js
|
Loading…
Reference in a new issue