Merge pull request #161 from anuragsoni/pascals-triangle

add pascals-triangle exercise
This commit is contained in:
Erik Simmler 2017-10-07 20:10:44 -04:00 committed by GitHub
commit b4c2346263
8 changed files with 164 additions and 0 deletions

View file

@ -321,6 +321,17 @@
"topics": [
]
},
{
"uuid": "a730ca85-057d-db80-e44e-25b8028acede6337cca",
"slug": "pascals-triangle",
"core": false,
"unlocked_by": null,
"difficulty": 4,
"topics": [
"Lists",
"Control-flow"
]
}
],
"foregone": [

View file

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

View file

@ -0,0 +1 @@
module Triangle exposing (..)

View file

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

View file

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

View file

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

View file

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

View file

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