ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# SergiiVlasiuk's solution

## to Pascal's Triangle in the Scala Track

Published at Aug 31 2019 · 1 comment
Instructions
Test suite
Solution

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.

``````    1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
# ... etc
``````

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test` in the exercise directory.

For more detailed info about the Scala track see the help page.

## Source

Pascal's Triangle at Wolfram Math World 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.

### PascalsTriangleTest.scala

``````import org.scalatest.{Matchers, FunSuite}

/** @version 1.3.0 */
class PascalsTriangleTest extends FunSuite with Matchers {

test("zero rows") {
PascalsTriangle.rows(0) should be(List())
}

test("single row") {
pending
PascalsTriangle.rows(1) should be(List(List(1)))
}

test("two rows") {
pending
PascalsTriangle.rows(2) should be(List(List(1), List(1, 1)))
}

test("three rows") {
pending
PascalsTriangle.rows(3) should be(List(List(1), List(1, 1), List(1, 2, 1)))
}

test("four rows") {
pending
PascalsTriangle.rows(4) should be(
List(List(1), List(1, 1), List(1, 2, 1), List(1, 3, 3, 1)))
}

test("five rows") {
pending
PascalsTriangle.rows(5) should be(
List(List(1),
List(1, 1),
List(1, 2, 1),
List(1, 3, 3, 1),
List(1, 4, 6, 4, 1)))
}

test("six rows") {
pending
PascalsTriangle.rows(6) should be(
List(List(1),
List(1, 1),
List(1, 2, 1),
List(1, 3, 3, 1),
List(1, 4, 6, 4, 1),
List(1, 5, 10, 10, 5, 1)))
}

test("ten rows") {
pending
PascalsTriangle.rows(10) should be(
List(
List(1),
List(1, 1),
List(1, 2, 1),
List(1, 3, 3, 1),
List(1, 4, 6, 4, 1),
List(1, 5, 10, 10, 5, 1),
List(1, 6, 15, 20, 15, 6, 1),
List(1, 7, 21, 35, 35, 21, 7, 1),
List(1, 8, 28, 56, 70, 56, 28, 8, 1),
List(1, 9, 36, 84, 126, 126, 84, 36, 9, 1)
))
}

test("negative rows") {
pending
PascalsTriangle.rows(-1) should be(List())
}
}``````
``````object PascalsTriangle {
private def createPyramide(actualRow: List[Long], nextRow: List[Long]): Stream[List[Long]] = {
val newRow = nextRow.head :: nextRow.zip(nextRow.tail).map(x => x._1 + x._2) ::: nextRow.last :: Nil
actualRow #:: createPyramide(nextRow, newRow)
}

val pyramid = createPyramide(List(1), List(1, 1))

def rows(lineNumber: Int): List[List[Long]] = pyramid.take(lineNumber).toList
}``````