[QUIZ] Second answer for quiz 84

After looking over my earlier answer, I realized that I had re-invented
String.center and had a number of excesses. This one is a lot smaller
but I
still feel I’m not using the language to its fullest.

JB

pp_pascal.rb

for details, see http://www.rubyquiz.com/quiz84.html

class Pascal
def initialize( depth )
@d = depth
@p = [[1]]
1.upto(@d-1) do |row|
@p[row] = [1]
1.upto(row-1) do |col|
@p[row] << (@p[row-1][col-1] + @p[row-1][col])
end
@p[row] << 1
end
end
def show
# the middle entry in the bottom row will have the
# most digits - use its length as the per-entry span
span = (@p[-1][(@d / 2).to_i].to_s.length) * 2
width = @d * span
rows = []
@p.each do |p|
ret = ‘’
p.each do |i|
ret += i.to_s.center(span)
end
rows << ret.center(width)
end
rows.join("\n")
end
end

default to 10 rows

rows = (ARGV[0] || 10).to_i
p = Pascal.new(rows)
puts p.show

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