diff --git a/config.json b/config.json index 9305e9d..a823c6f 100644 --- a/config.json +++ b/config.json @@ -22,6 +22,7 @@ "run-length-encoding", "sublist", "nucleotide-count", + "series", "phone-number", "grade-school" ], diff --git a/elm-package.json b/elm-package.json index e6a166d..26434c3 100644 --- a/elm-package.json +++ b/elm-package.json @@ -23,6 +23,7 @@ "./exercises/strain", "./exercises/space-age", "./exercises/nucleotide-count", + "./exercises/series", "./exercises/phone-number", "./exercises/grade-school" ], diff --git a/exercises/series/Series.elm b/exercises/series/Series.elm new file mode 100644 index 0000000..43ea726 --- /dev/null +++ b/exercises/series/Series.elm @@ -0,0 +1 @@ +module Series (..) where diff --git a/exercises/series/Series.example b/exercises/series/Series.example new file mode 100644 index 0000000..51673fe --- /dev/null +++ b/exercises/series/Series.example @@ -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 []) diff --git a/exercises/series/SeriesTests.elm b/exercises/series/SeriesTests.elm new file mode 100644 index 0000000..25acea7 --- /dev/null +++ b/exercises/series/SeriesTests.elm @@ -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) diff --git a/exercises/series/elm-package.json b/exercises/series/elm-package.json new file mode 100644 index 0000000..d93a035 --- /dev/null +++ b/exercises/series/elm-package.json @@ -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" +}