Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Pascal's Triangle in the Ruby Track

Published at Jul 13 2018 · 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

For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby pascals_triangle_test.rb

To include color from the command line:

ruby -r minitest/pride pascals_triangle_test.rb

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

require 'minitest/autorun'
require_relative 'pascals_triangle'

class TriangleTest < Minitest::Test
  def test_one_row
    triangle = Triangle.new(1)
    assert_equal [[1]], triangle.rows
  end

  def test_two_rows
    skip
    triangle = Triangle.new(2)
    assert_equal [[1], [1, 1]], triangle.rows
  end

  def test_three_rows
    skip
    triangle = Triangle.new(3)
    assert_equal [[1], [1, 1], [1, 2, 1]], triangle.rows
  end

  def test_fourth_row
    skip
    triangle = Triangle.new(4)
    assert_equal [1, 3, 3, 1], triangle.rows.last
  end

  def test_fifth_row
    skip
    triangle = Triangle.new(5)
    assert_equal [1, 4, 6, 4, 1], triangle.rows.last
  end

  def test_twentieth_row
    skip
    triangle = Triangle.new(20)
    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_equal expected, triangle.rows.last
  end
end
# Pascals Triangle
class PascalsTriangle
  attr_reader :num_rows
  def initialize(num_rows)
    @num_rows = num_rows
  end

  def rows
    (0..(num_rows - 1)).map { |row_num| build_row(row_num) }
  end

  def build_row(row_num)
    (0..row_num).map { |col_num| n_choose_k(row_num, col_num) }
  end

  private

  def n_choose_k(n, k)
    factorial(n) / (factorial(k) * factorial(n - k))
  end

  def factorial(n)
    return 1 if n == 0
    n.downto(1).reduce(:*)
  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?