Created
March 9, 2017 14:59
-
-
Save abrahamsangha/8b3dd3d1da0c729ff29768e3b827f867 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Triangle | |
attr_reader :number_of_rows | |
def initialize(number_of_rows) | |
@number_of_rows = number_of_rows | |
end | |
def rows | |
if number_of_rows == 1 | |
[[1]] | |
else | |
top_triangle = Triangle.new(number_of_rows - 1) | |
previous_row = top_triangle.rows[-1] | |
new_row = Array.new(previous_row.size + 1) do |index| | |
case | |
when index == 0 || index == previous_row.size | |
1 | |
else | |
previous_row[index - 1] + previous_row[index] | |
end | |
end | |
top_triangle.rows << new_row | |
end | |
end | |
# def initialize(num) | |
# @rows = [[1]] | |
# counter = 1 | |
# until counter == num | |
# prev_row = @rows[-1] | |
# counter += 1 | |
# new_row = Array.new(counter) do |index| | |
# case | |
# when index == 0 || index == counter - 1 | |
# 1 | |
# else | |
# prev_row[index - 1] + prev_row[index] | |
# end | |
# end | |
# @rows << new_row | |
# end | |
# end | |
# | |
# def rows | |
# @rows | |
# end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment