(no subject)

#!/usr/bin/ruby
module Pascal
module_function
def next_row®
[1] + (0…r.size-1).map{|i|r[i]+r[i+1]} + [1]
end

def pyramid(n)
n==1 ? [[1]] : (p=pyramid(n-1)) << next_row(p[-1])
end

def pyramid_string§
number_spacing = 1
slot_width = p[-1].map{|n|n.to_s.size}.max + number_spacing
row_width = slot_width * p[-1].size
p.map {|row| row.map {|n| n.to_s.center(slot_width)}.join.center
(row_width)}.join("\n")
end
end

if FILE==$0
puts Pascal::pyramid_string(Pascal::pyramid(Integer(ARGV[0]) || 10))
end
pascal.rb (END)

Sorry - I thought it was Sunday!

Please accept my apology if I’ve spoilt it for you :frowning:

Sorry,
Ben

$ irb
irb(main):001:0> (Time.now - first_post.time) > 48.hours
=> false

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