Created
January 19, 2018 17:54
-
-
Save dudo/efc431d62c115dac9d6fe28f9fffc46e 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 PascalTriangle | |
attr_accessor :height | |
def initialize(height) | |
@height = height.to_i | |
end | |
def self.next_row(current_row) | |
([0] + current_row).zip(current_row + [0]).map{ |a| a.inject(:+) } | |
end | |
def draw | |
current_row = [1] # always start at the top | |
height.times do |i| | |
puts current_row.join(' ') | |
current_row = self.class.next_row(current_row) | |
end | |
end | |
def self.tests_passing? | |
[ | |
self.next_row([1]) == [1,1], | |
self.next_row([1,1]) == [1,2,1], | |
self.next_row([1,2,1]) == [1,3,3,1] | |
].all? | |
end | |
end | |
raise 'BadTriangle' unless PascalTriangle.tests_passing? | |
PascalTriangle.new(ARGV[0]).draw |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment