Avatar of paulfioravanti

paulfioravanti's solution

to Pascal's Triangle in the Ruby Track

Published at May 21 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

For installation and learning resources, refer to the Ruby resources 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
class Triangle
  # 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 initialize(num_rows)
    @max_row = num_rows - 1
  end

  def rows
    (0..max_row).map(&method(:generate_row))
  end

  private

  attr_reader :max_row

  def generate_row(row_num)
    (0..row_num)
      .each
      .with_object(row_num)
      .map(&method(:binomial))
  end

  # https://en.wikipedia.org/wiki/Binomial_theorem
  # "n (row_num) choose k (exponent)" => n!/(n - k)!k!
  def binomial(exponent, row_num)
    factorial(row_num) / (factorial(row_num - exponent) * factorial(exponent))
  end

  # Provide starting accumulator for 0! == 1
  def factorial(num)
    (1..num).reduce(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?