mirror of
https://github.com/correl/elm.git
synced 2024-12-18 11:06:17 +00:00
Add say exercise
This commit is contained in:
parent
72a56f6e12
commit
550b9e1f87
8 changed files with 242 additions and 2 deletions
|
@ -28,7 +28,8 @@
|
|||
"allergies",
|
||||
"robot-simulator",
|
||||
"list-ops",
|
||||
"atbash-cipher"
|
||||
"atbash-cipher",
|
||||
"say"
|
||||
],
|
||||
"deprecated": [
|
||||
|
||||
|
|
|
@ -29,7 +29,8 @@
|
|||
"./exercises/allergies",
|
||||
"./exercises/robot-simulator",
|
||||
"./exercises/list-ops",
|
||||
"./exercises/atbash-cipher"
|
||||
"./exercises/atbash-cipher",
|
||||
"./exercises/say"
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
|
|
1
exercises/say/Say.elm
Normal file
1
exercises/say/Say.elm
Normal file
|
@ -0,0 +1 @@
|
|||
module Say exposing (say, SayError(..))
|
126
exercises/say/Say.example
Normal file
126
exercises/say/Say.example
Normal file
|
@ -0,0 +1,126 @@
|
|||
module Say exposing (say, SayError(..))
|
||||
|
||||
import Array
|
||||
import Dict
|
||||
|
||||
|
||||
type SayError
|
||||
= Negative
|
||||
| TooLarge
|
||||
|
||||
|
||||
say : Int -> Result SayError String
|
||||
say number =
|
||||
if number < 0 then
|
||||
Err Negative
|
||||
else if number < 20 then
|
||||
Ok (saySmall number)
|
||||
else if number < 100 then
|
||||
if number % 10 == 0 then
|
||||
Ok (saySmall number)
|
||||
else
|
||||
Ok (sayTens (number // 10 * 10) ++ "-" ++ (saySmall (number % 10)))
|
||||
else if number < 1000 then
|
||||
Ok (sayLarge number 100 "hundred")
|
||||
else if number < million then
|
||||
Ok (sayLarge number 1000 "thousand")
|
||||
else if number < billion then
|
||||
Ok (sayLarge number million "million")
|
||||
else if number < trillion then
|
||||
Ok (sayLarge number billion "billion")
|
||||
else if number < quintillion then
|
||||
Ok (sayLarge number trillion "trillion")
|
||||
else
|
||||
Err TooLarge
|
||||
|
||||
|
||||
safeSay : Int -> String
|
||||
safeSay number =
|
||||
say number |> Result.withDefault ""
|
||||
|
||||
|
||||
andSay : Int -> String
|
||||
andSay number =
|
||||
if number < 100 then
|
||||
"and " ++ safeSay number
|
||||
else
|
||||
safeSay number
|
||||
|
||||
|
||||
saySmall : Int -> String
|
||||
saySmall number =
|
||||
Array.get number smalls |> Maybe.withDefault ""
|
||||
|
||||
|
||||
sayTens : Int -> String
|
||||
sayTens number =
|
||||
Dict.get (number) tens |> Maybe.withDefault ""
|
||||
|
||||
|
||||
sayLarge : Int -> Int -> String -> String
|
||||
sayLarge number large name =
|
||||
if number % large == 0 then
|
||||
safeSay (number // large) ++ " " ++ name
|
||||
else
|
||||
safeSay (number // large) ++ " " ++ name ++ " " ++ (andSay (number % large))
|
||||
|
||||
|
||||
smalls : Array.Array String
|
||||
smalls =
|
||||
Array.fromList
|
||||
[ "zero"
|
||||
, "one"
|
||||
, "two"
|
||||
, "three"
|
||||
, "four"
|
||||
, "five"
|
||||
, "six"
|
||||
, "seven"
|
||||
, "eight"
|
||||
, "nine"
|
||||
, "ten"
|
||||
, "eleven"
|
||||
, "twelve"
|
||||
, "thirteen"
|
||||
, "fourteen"
|
||||
, "fifteen"
|
||||
, "sixteen"
|
||||
, "seventeen"
|
||||
, "eighteen"
|
||||
, "nineteen"
|
||||
, "twenty"
|
||||
]
|
||||
|
||||
|
||||
tens : Dict.Dict Int String
|
||||
tens =
|
||||
Dict.fromList
|
||||
[ ( 20, "twenty" )
|
||||
, ( 30, "thirty" )
|
||||
, ( 40, "forty" )
|
||||
, ( 50, "fifty" )
|
||||
, ( 60, "sixty" )
|
||||
, ( 70, "seventy" )
|
||||
, ( 80, "eighty" )
|
||||
, ( 90, "ninety" )
|
||||
]
|
||||
|
||||
|
||||
million : Int
|
||||
million =
|
||||
1000000
|
||||
|
||||
|
||||
billion : Int
|
||||
billion =
|
||||
1000000000
|
||||
|
||||
|
||||
trillion : Int
|
||||
trillion =
|
||||
1000000000000
|
||||
|
||||
|
||||
quintillion : Int
|
||||
quintillion =
|
||||
1000000000000000
|
90
exercises/say/SayTests.elm
Normal file
90
exercises/say/SayTests.elm
Normal file
|
@ -0,0 +1,90 @@
|
|||
module Main exposing (..)
|
||||
|
||||
import ElmTest exposing (..)
|
||||
import Say exposing (say, SayError(Negative, TooLarge))
|
||||
|
||||
|
||||
tests : Test
|
||||
tests =
|
||||
suite "Series"
|
||||
[ test "one"
|
||||
(assertEqual (Ok "one")
|
||||
(say 1)
|
||||
)
|
||||
, test "fourteen"
|
||||
(assertEqual (Ok "fourteen")
|
||||
(say 14)
|
||||
)
|
||||
, test "twenty"
|
||||
(assertEqual (Ok "twenty")
|
||||
(say 20)
|
||||
)
|
||||
, test "twenty-two"
|
||||
(assertEqual (Ok "twenty-two")
|
||||
(say 22)
|
||||
)
|
||||
, test "one hundred"
|
||||
(assertEqual (Ok "one hundred")
|
||||
(say 100)
|
||||
)
|
||||
, test "one hundred twenty"
|
||||
(assertEqual (Ok "one hundred and twenty")
|
||||
(say 120)
|
||||
)
|
||||
, test "one hundred twenty-three"
|
||||
(assertEqual (Ok "one hundred and twenty-three")
|
||||
(say 123)
|
||||
)
|
||||
, test "one thousand"
|
||||
(assertEqual (Ok "one thousand")
|
||||
(say 1000)
|
||||
)
|
||||
, test "one thousand two hundred thirty-four"
|
||||
(assertEqual (Ok "one thousand two hundred and thirty-four")
|
||||
(say 1234)
|
||||
)
|
||||
, test "one million"
|
||||
(assertEqual (Ok "one million")
|
||||
(say 1000000)
|
||||
)
|
||||
, test "one million two"
|
||||
(assertEqual (Ok "one million and two")
|
||||
(say 1000002)
|
||||
)
|
||||
, test "1002345"
|
||||
(assertEqual (Ok "one million two thousand three hundred and forty-five")
|
||||
(say 1002345)
|
||||
)
|
||||
, test "one billion"
|
||||
(assertEqual (Ok "one billion")
|
||||
(say 1000000000)
|
||||
)
|
||||
, test "number too large"
|
||||
(assertEqual (Err TooLarge)
|
||||
(say 10000000000000000)
|
||||
)
|
||||
, test "negative number"
|
||||
(assertEqual (Err Negative)
|
||||
(say -42)
|
||||
)
|
||||
, test "zero"
|
||||
(assertEqual (Ok "zero")
|
||||
(say 0)
|
||||
)
|
||||
, test "987654321123"
|
||||
(assertEqual
|
||||
(Ok
|
||||
("nine hundred and eighty-seven billion "
|
||||
++ "six hundred and fifty-four million "
|
||||
++ "three hundred and twenty-one thousand "
|
||||
++ "one hundred and twenty-three"
|
||||
)
|
||||
)
|
||||
(say 987654321123)
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
main : Program Never
|
||||
main =
|
||||
runSuite tests
|
15
exercises/say/elm-package.json
Normal file
15
exercises/say/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/say/runtests.bat
Executable file
4
exercises/say/runtests.bat
Executable 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/say/runtests.sh
Executable file
2
exercises/say/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