From bf41b24bf40ee487e5818f081c08b0561a0fd345 Mon Sep 17 00:00:00 2001 From: Erik Simmler Date: Sat, 12 Mar 2016 20:08:48 -0500 Subject: [PATCH] Add difference-of-squares exercise (closes #27) --- config.json | 3 +- elm-package.json | 3 +- .../DifferenceOfSquares.elm | 1 + .../DifferenceOfSquares.example | 19 ++++++++++ .../DifferenceOfSquaresTests.elm | 37 +++++++++++++++++++ .../difference-of-squares/elm-package.json | 16 ++++++++ 6 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 exercises/difference-of-squares/DifferenceOfSquares.elm create mode 100644 exercises/difference-of-squares/DifferenceOfSquares.example create mode 100644 exercises/difference-of-squares/DifferenceOfSquaresTests.elm create mode 100644 exercises/difference-of-squares/elm-package.json diff --git a/config.json b/config.json index 25840b7..1a7ad6b 100644 --- a/config.json +++ b/config.json @@ -12,7 +12,8 @@ "hamming", "word-count", "bob", - "run-length-encoding" + "run-length-encoding", + "difference-of-squares" ], "deprecated": [ diff --git a/elm-package.json b/elm-package.json index 94692b7..015d5d6 100644 --- a/elm-package.json +++ b/elm-package.json @@ -12,7 +12,8 @@ "./exercises/hamming", "./exercises/word-count", "./exercises/bob", - "./exercises/run-length-encoding" + "./exercises/run-length-encoding", + "./exercises/difference-of-squares" ], "exposed-modules": [], "dependencies": { diff --git a/exercises/difference-of-squares/DifferenceOfSquares.elm b/exercises/difference-of-squares/DifferenceOfSquares.elm new file mode 100644 index 0000000..1da591a --- /dev/null +++ b/exercises/difference-of-squares/DifferenceOfSquares.elm @@ -0,0 +1 @@ +module DifferenceOfSquares (..) where diff --git a/exercises/difference-of-squares/DifferenceOfSquares.example b/exercises/difference-of-squares/DifferenceOfSquares.example new file mode 100644 index 0000000..518c070 --- /dev/null +++ b/exercises/difference-of-squares/DifferenceOfSquares.example @@ -0,0 +1,19 @@ +module DifferenceOfSquares (..) where + + +squareOfSum : Int -> Int +squareOfSum n = + let + sum = n * (n + 1) // 2 + in + sum * sum + + +sumOfSquares : Int -> Int +sumOfSquares n = + List.sum (List.map (\m -> m * m) [0..n]) + + +difference : Int -> Int +difference n = + squareOfSum n - sumOfSquares n diff --git a/exercises/difference-of-squares/DifferenceOfSquaresTests.elm b/exercises/difference-of-squares/DifferenceOfSquaresTests.elm new file mode 100644 index 0000000..1653db8 --- /dev/null +++ b/exercises/difference-of-squares/DifferenceOfSquaresTests.elm @@ -0,0 +1,37 @@ +module Main (..) where + +import Task +import Console +import ElmTest exposing (..) +import DifferenceOfSquares exposing (squareOfSum, sumOfSquares, difference) + + +tests : Test +tests = + suite + "DifferenceOfSquares" + [ suite + "square the sum of the numbers up to the given number" + [ test "square of sum 5" (assertEqual 225 (squareOfSum 5)) + , test "square of sum 10" (assertEqual 3025 (squareOfSum 10)) + , test "square of sum 100" (assertEqual 25502500 (squareOfSum 100)) + ] + , suite + "sum the squares of the numbers up to the given number" + [ test "sum of squares 5" (assertEqual 55 (sumOfSquares 5)) + , test "sum of squares 10" (assertEqual 385 (sumOfSquares 10)) + , test "sum of squares 100" (assertEqual 338350 (sumOfSquares 100)) + ] + , suite + "subtract sum of squares from square of sums" + [ test "difference of squares 0" (assertEqual 0 (difference 0)) + , test "difference of squares 5" (assertEqual 170 (difference 5)) + , test "difference of squares 10" (assertEqual 2640 (difference 10)) + , test "difference of squares 100" (assertEqual 25164150 (difference 100)) + ] + ] + + +port runner : Signal (Task.Task x ()) +port runner = + Console.run (consoleRunner tests) diff --git a/exercises/difference-of-squares/elm-package.json b/exercises/difference-of-squares/elm-package.json new file mode 100644 index 0000000..d93a035 --- /dev/null +++ b/exercises/difference-of-squares/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" +}