mirror of
https://github.com/correl/elm.git
synced 2024-12-23 03:00:08 +00:00
Merge pull request #161 from anuragsoni/pascals-triangle
add pascals-triangle exercise
This commit is contained in:
commit
b4c2346263
8 changed files with 164 additions and 0 deletions
11
config.json
11
config.json
|
@ -321,6 +321,17 @@
|
||||||
"topics": [
|
"topics": [
|
||||||
|
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"uuid": "a730ca85-057d-db80-e44e-25b8028acede6337cca",
|
||||||
|
"slug": "pascals-triangle",
|
||||||
|
"core": false,
|
||||||
|
"unlocked_by": null,
|
||||||
|
"difficulty": 4,
|
||||||
|
"topics": [
|
||||||
|
"Lists",
|
||||||
|
"Control-flow"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"foregone": [
|
"foregone": [
|
||||||
|
|
51
exercises/pascals-triangle/README.md
Normal file
51
exercises/pascals-triangle/README.md
Normal 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.
|
1
exercises/pascals-triangle/Triangle.elm
Normal file
1
exercises/pascals-triangle/Triangle.elm
Normal file
|
@ -0,0 +1 @@
|
||||||
|
module Triangle exposing (..)
|
23
exercises/pascals-triangle/Triangle.example.elm
Normal file
23
exercises/pascals-triangle/Triangle.example.elm
Normal 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 ]
|
14
exercises/pascals-triangle/elm-package.json
Normal file
14
exercises/pascals-triangle/elm-package.json
Normal 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"
|
||||||
|
}
|
14
exercises/pascals-triangle/package.json
Normal file
14
exercises/pascals-triangle/package.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
34
exercises/pascals-triangle/tests/Tests.elm
Normal file
34
exercises/pascals-triangle/tests/Tests.elm
Normal 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)
|
||||||
|
]
|
16
exercises/pascals-triangle/tests/elm-package.json
Normal file
16
exercises/pascals-triangle/tests/elm-package.json
Normal 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"
|
||||||
|
}
|
Loading…
Reference in a new issue