diff --git a/config.json b/config.json index e3a670d..8179930 100644 --- a/config.json +++ b/config.json @@ -330,6 +330,17 @@ "topics": [ ] + }, + { + "uuid": "a730ca85-057d-db80-e44e-25b8028acede6337cca", + "slug": "pascals-triangle", + "core": false, + "unlocked_by": null, + "difficulty": 4, + "topics": [ + "Lists", + "Control-flow" + ] } ], "foregone": [ diff --git a/exercises/pascals-triangle/README.md b/exercises/pascals-triangle/README.md new file mode 100644 index 0000000..f83ce46 --- /dev/null +++ b/exercises/pascals-triangle/README.md @@ -0,0 +1,51 @@ +# Pascals Triangle + +Compute Pascal's triangle up to a given number of rows. + +In Pascal's Triangle each number is computed by adding the numbers to +the right and left of the current position in the previous row. + +```text + 1 + 1 1 + 1 2 1 + 1 3 3 1 +1 4 6 4 1 +# ... etc +``` + +## Elm Installation + +Refer to the [Exercism help page](http://exercism.io/languages/elm) for Elm +installation and learning resources. + +## Writing the Code + +The first time you start an exercise, you'll need to ensure you have the +appropriate dependencies installed. + +```bash +$ npm install +``` + +Execute the tests with: + +```bash +$ npm test +``` + +Automatically run tests again when you save changes: + +```bash +$ npm run watch +``` + +As you work your way through the test suite, be sure to remove the `skip <|` +calls from each test until you get them all passing! + +## Source + +Pascal's Triangle at Wolfram Math World [http://mathworld.wolfram.com/PascalsTriangle.html](http://mathworld.wolfram.com/PascalsTriangle.html) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/pascals-triangle/Triangle.elm b/exercises/pascals-triangle/Triangle.elm new file mode 100644 index 0000000..0af09a8 --- /dev/null +++ b/exercises/pascals-triangle/Triangle.elm @@ -0,0 +1 @@ +module Triangle exposing (..) diff --git a/exercises/pascals-triangle/Triangle.example.elm b/exercises/pascals-triangle/Triangle.example.elm new file mode 100644 index 0000000..b86a756 --- /dev/null +++ b/exercises/pascals-triangle/Triangle.example.elm @@ -0,0 +1,23 @@ +module Triangle exposing (rows) + +import List + + +nextRow : List Int -> List Int +nextRow row = + List.map2 (+) (0 :: row) (row ++ [ 0 ]) + + +rows : Int -> List (List Int) +rows n = + let + loop i row = + if i == n then + [] + else + row :: loop (i + 1) (nextRow row) + in + if n < 0 then + [] + else + loop 0 [ 1 ] diff --git a/exercises/pascals-triangle/elm-package.json b/exercises/pascals-triangle/elm-package.json new file mode 100644 index 0000000..132e19c --- /dev/null +++ b/exercises/pascals-triangle/elm-package.json @@ -0,0 +1,14 @@ +{ + "version": "3.0.0", + "summary": "Exercism problems in Elm.", + "repository": "https://github.com/exercism/elm.git", + "license": "BSD3", + "source-directories": [ + "." + ], + "exposed-modules": [], + "dependencies": { + "elm-lang/core": "5.0.0 <= v < 6.0.0" + }, + "elm-version": "0.18.0 <= v < 0.19.0" +} diff --git a/exercises/pascals-triangle/package.json b/exercises/pascals-triangle/package.json new file mode 100644 index 0000000..31d6fb3 --- /dev/null +++ b/exercises/pascals-triangle/package.json @@ -0,0 +1,14 @@ +{ + "description": "Exercism/Elm", + "repository": "https://github.com/exercism/elm.git", + "license": "MIT", + "scripts": { + "postinstall": "elm-package install -y", + "watch": "elm-test --watch", + "test": "elm-test" + }, + "dependencies": { + "elm": "0.18.0", + "elm-test": "0.18.3" + } +} diff --git a/exercises/pascals-triangle/tests/Tests.elm b/exercises/pascals-triangle/tests/Tests.elm new file mode 100644 index 0000000..210b2cd --- /dev/null +++ b/exercises/pascals-triangle/tests/Tests.elm @@ -0,0 +1,34 @@ +module Tests exposing (..) + +import Expect +import Test exposing (..) +import Triangle exposing (rows) + + +tests : Test +tests = + describe "Triangle" + [ test "no rows" <| + \() -> + Expect.equal [] (rows 0) + , skip <| + test "single row" <| + \() -> + Expect.equal [ [ 1 ] ] (rows 1) + , skip <| + test "two rows" <| + \() -> + Expect.equal [ [ 1 ], [ 1, 1 ] ] (rows 2) + , skip <| + test "three rows" <| + \() -> + Expect.equal [ [ 1 ], [ 1, 1 ], [ 1, 2, 1 ] ] (rows 3) + , skip <| + test "four rows" <| + \() -> + Expect.equal [ [ 1 ], [ 1, 1 ], [ 1, 2, 1 ], [ 1, 3, 3, 1 ] ] (rows 4) + , skip <| + test "negative rows" <| + \() -> + Expect.equal [] (rows -1) + ] diff --git a/exercises/pascals-triangle/tests/elm-package.json b/exercises/pascals-triangle/tests/elm-package.json new file mode 100644 index 0000000..3e92515 --- /dev/null +++ b/exercises/pascals-triangle/tests/elm-package.json @@ -0,0 +1,16 @@ +{ + "version": "3.0.0", + "summary": "Exercism problems in Elm.", + "repository": "https://github.com/exercism/elm.git", + "license": "BSD3", + "source-directories": [ + ".", + ".." + ], + "exposed-modules": [], + "dependencies": { + "elm-lang/core": "5.0.0 <= v < 6.0.0", + "elm-community/elm-test": "4.0.0 <= v < 5.0.0" + }, + "elm-version": "0.18.0 <= v < 0.19.0" +}