Skip to content

Instantly share code, notes, and snippets.

@dudo
Created January 19, 2018 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dudo/efc431d62c115dac9d6fe28f9fffc46e to your computer and use it in GitHub Desktop.
Save dudo/efc431d62c115dac9d6fe28f9fffc46e to your computer and use it in GitHub Desktop.
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