1
0
Fork 0
mirror of https://github.com/correl/elm.git synced 2024-12-22 19:17:27 +00:00

Add series exercise

This commit is contained in:
Erik Simmler 2016-03-27 07:29:28 -04:00
parent 919cc6a2ba
commit 2325e7d6ef
6 changed files with 118 additions and 0 deletions

View file

@ -22,6 +22,7 @@
"run-length-encoding", "run-length-encoding",
"sublist", "sublist",
"nucleotide-count", "nucleotide-count",
"series",
"phone-number", "phone-number",
"grade-school" "grade-school"
], ],

View file

@ -23,6 +23,7 @@
"./exercises/strain", "./exercises/strain",
"./exercises/space-age", "./exercises/space-age",
"./exercises/nucleotide-count", "./exercises/nucleotide-count",
"./exercises/series",
"./exercises/phone-number", "./exercises/phone-number",
"./exercises/grade-school" "./exercises/grade-school"
], ],

View file

@ -0,0 +1 @@
module Series (..) where

View file

@ -0,0 +1,33 @@
module Series (..) where
import String
import List
import Result
slices : Int -> String -> Result String (List (List Int))
slices size input =
if size < 1 then
Err ("Invalid size: " ++ toString size)
else
String.split "" input
|> List.map String.toInt
|> combine
|> Result.map (takeRuns size)
takeRuns : Int -> List Int -> List (List Int)
takeRuns size numbers =
let
candidate =
List.take size numbers
in
if List.length candidate < size || size < 1 then
[]
else
candidate :: takeRuns size (List.drop 1 numbers)
combine : List (Result x a) -> Result x (List a)
combine =
List.foldr (Result.map2 (::)) (Ok [])

View file

@ -0,0 +1,66 @@
module Main (..) where
import Task
import Console
import ElmTest exposing (..)
import Series exposing (slices)
tests : Test
tests =
suite
"Series"
[ test
"slices of one"
(assertEqual
(Ok [ [ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ] ])
(slices 1 "01234")
)
, test
"slices of two"
(assertEqual
(Ok [ [ 9, 7 ], [ 7, 8 ], [ 8, 6 ], [ 6, 7 ], [ 7, 5 ], [ 5, 6 ], [ 6, 4 ] ])
(slices 2 "97867564")
)
, test
"slices of three"
(assertEqual
(Ok [ [ 9, 7, 8 ], [ 7, 8, 6 ], [ 8, 6, 7 ], [ 6, 7, 5 ], [ 7, 5, 6 ], [ 5, 6, 4 ] ])
(slices 3 "97867564")
)
, test
"slices of four"
(assertEqual
(Ok [ [ 0, 1, 2, 3 ], [ 1, 2, 3, 4 ] ])
(slices 4 "01234")
)
, test
"slices of five"
(assertEqual
(Ok [ [ 0, 1, 2, 3, 4 ] ])
(slices 5 "01234")
)
, test
"overly long slice"
(assertEqual
(Ok [])
(slices 4 "012")
)
, test
"overly short slice"
(assertEqual
(Err ("Invalid size: 0"))
(slices 0 "01234")
)
, test
"input has non numbers"
(assertEqual
(Err "could not convert string 'a' to an Int")
(slices 2 "0123abc")
)
]
port runner : Signal (Task.Task x ())
port runner =
Console.run (consoleRunner tests)

View file

@ -0,0 +1,16 @@
{
"version": "1.0.0",
"summary": "Exercism problems in Elm.",
"repository": "https://github.com/exercism/xelm.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"deadfoxygrandpa/elm-test": "3.0.1 <= v < 4.0.0",
"elm-lang/core": "2.0.0 <= v < 4.0.0",
"laszlopandy/elm-console": "1.1.0 <= v < 2.0.0"
},
"elm-version": "0.15.0 <= v < 0.17.0"
}