Add difference-of-squares exercise (closes #27)

This commit is contained in:
Erik Simmler 2016-03-12 20:08:48 -05:00
parent f39fc467e3
commit bf41b24bf4
6 changed files with 77 additions and 2 deletions

View file

@ -12,7 +12,8 @@
"hamming",
"word-count",
"bob",
"run-length-encoding"
"run-length-encoding",
"difference-of-squares"
],
"deprecated": [

View file

@ -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": {

View file

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

View file

@ -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

View file

@ -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)

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"
}