#84 Pascal Triangle

hi everyone,
here’s my own solution to the problem:

pascal.rb

class Pascal
attr_reader :triangle

def initialize(rows)
@triangle=[]
generate(rows)
end

def [](row, n)
if row>0 and row<[email protected] and n>0 and n<=
@triangle[row-1].length
@triangle[row-1][n-1]
else
nil
end
end

def generate(rows)
return nil if rows<@triangle.length
rows.times do |s|
row=[1]
1.upto(s-1) do |n|
row[n][email protected][s-1][n-1][email protected][s-1][n]
end
row[s]=1
@triangle << row
end
end

def to_s
[email protected]_s.length
total_length=(spacing+1)*@triangle.last.length
@triangle.map do |row|
row.map do |number|
number.to_s.center(spacing)
end.join(" “).center(total_length)
end.join(”\n")
end

end

puts Pascal.new(ARGV[0].to_i)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs