🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of altuntasfatih

altuntasfatih's solution

to Pascal's Triangle in the Elixir Track

Published at May 23 2020 · 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

Running tests

Execute the tests with:

$ mix test

Pending tests

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.

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.

pascals_triangle_test.exs

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

test_helper.exs

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

lib/pascals_triangle.ex

defmodule PascalsTriangle do
  @doc """
  Calculates the rows of a pascal triangle
  with the given height
  test result of solution_one vs solution two

  solution_one ->
  PascalsTriangleTest
  * test fifth row (4.0ms)
  * test 5000 row (12057.8ms)
  * test one row (0.00ms)
  * test 2000 row (1651.0ms)
  * test twentieth row (0.07ms)
  * test three rows (0.00ms)
  * test two rows (0.00ms)
  * test fourth row (0.01ms)
  Finished in 13.7 seconds
  8 tests, 0 failures

  solution_two ->
  PascalsTriangleTest
  * test one row (1.3ms)
  * test 5000 row (16807.6ms)
  * test fifth row (0.04ms)
  * test fourth row (0.01ms)
  * test twentieth row (0.08ms)
  * test 2000 row (1832.1ms)
  * test three rows (0.01ms)
  * test two rows (0.01ms)
  Finished in 18.7 seconds
  8 tests, 0 failures

  """
  @spec rows(integer) :: [[integer]]
  def rows(num) do
    solution_one(num)
  end

  def solution_two(num) do
    Stream.iterate([1], fn x ->
      x = [0 | x]

      Enum.chunk_every(x, 2, 1, [0])
      |> Enum.map(&Enum.sum/1)
    end)
    |> Enum.take(num)
  end

  def solution_one(num) do
    Enum.to_list(1..num)
    |> Enum.reduce([], fn r, acc ->
      trinangle(r, acc)
    end)
    |> Enum.reverse()
  end

  def trinangle(1, []), do: [[1]]

  def trinangle(_, [head | _] = acc) do
    sub =
      Enum.chunk_every(head, 2, 1, [0])
      |> Enum.map(&Enum.sum/1)

    [[1 | sub] | acc]
  end
end

test/pascals_triangle_test.exs

defmodule PascalsTriangleTest do
  use ExUnit.Case

  test "one row" do
    assert PascalsTriangle.rows(1) == [[1]]
  end

  test "two rows" do
    assert PascalsTriangle.rows(2) == [[1], [1, 1]]
  end

  test "three rows" do
    assert PascalsTriangle.rows(3) == [[1], [1, 1], [1, 2, 1]]
  end

  test "fourth row" do
    assert List.last(PascalsTriangle.rows(4)) == [1, 3, 3, 1]
  end

  test "fifth row" do
    assert List.last(PascalsTriangle.rows(5)) == [1, 4, 6, 4, 1]
  end

  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

  test "2000 row" do
    assert List.first(PascalsTriangle.rows(2000)) == [1]
  end

  test "5000 row" do
    assert List.first(PascalsTriangle.rows(5000)) == [1]
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?