Published at Aug 10 2019
·
0 comments

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

Execute the tests with:

```
$ mix test
```

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by
commenting out the relevant `@tag :pending`

with a `#`

symbol.

For example:

```
# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
```

Or, you can enable all the tests by commenting out the
`ExUnit.configure`

line in the test suite.

```
# ExUnit.configure exclude: :pending, trace: true
```

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

Pascal's Triangle at Wolfram Math World http://mathworld.wolfram.com/PascalsTriangle.html

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
defmodule PascalsTriangleTest do
use ExUnit.Case
# @tag pending
test "one row" do
assert PascalsTriangle.rows(1) == [[1]]
end
@tag :pending
test "two rows" do
assert PascalsTriangle.rows(2) == [[1], [1, 1]]
end
@tag :pending
test "three rows" do
assert PascalsTriangle.rows(3) == [[1], [1, 1], [1, 2, 1]]
end
@tag :pending
test "fourth row" do
assert List.last(PascalsTriangle.rows(4)) == [1, 3, 3, 1]
end
@tag :pending
test "fifth row" do
assert List.last(PascalsTriangle.rows(5)) == [1, 4, 6, 4, 1]
end
@tag :pending
test "twentieth row" do
expected = [
1,
19,
171,
969,
3876,
11_628,
27_132,
50_388,
75_582,
92_378,
92_378,
75_582,
50_388,
27_132,
11_628,
3876,
969,
171,
19,
1
]
assert List.last(PascalsTriangle.rows(20)) == expected
end
end
```

```
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
```

```
defmodule PascalsTriangle do
@doc """
Calculates the rows of a pascal triangle
with the given height
"""
@spec rows(integer) :: [[integer]]
# https://en.wikipedia.org/wiki/Pascal%27s_triangle
# "The rows of Pascal's triangle are conventionally enumerated
# starting with row n = 0 at the top (the 0th row)", so instantly decrement
# the number of rows by 1.
def rows(num), do: Enum.map(0..(num - 1), &generate_row/1)
defp generate_row(row_num), do: Enum.map(0..row_num, &binomial(&1, row_num))
# https://en.wikipedia.org/wiki/Binomial_theorem
# "n (row_num) choose k (exponent)" => n!/(n - k)!k!
defp binomial(exponent, row_num) do
factorial(row_num) / (factorial(row_num - exponent) * factorial(exponent))
end
defp factorial(0), do: 1
defp factorial(num), do: Enum.reduce(1..num, 1, &*/2)
end
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,373 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments