Hello everyone! This is my first quiz submission.
I suppose it's a little verbose, but I feel it's clever enough. I do
the straightforward, plain-vanilla recursive building of the triangle
right from the Array constructor, which is kinda cool, then extend the
Array class to print it out. (The to_centered_spaced_string method
really only works on a fairly specific subset of 2D arrays,
unfortunately.)
And, thanks for the quizzes! I have enjoyed the past entries greatly.
Devin Chalmers
PS - sorry for the 81-char line. I hate it too.
==== pp_pascal.rb
#!/usr/local/bin/ruby
def pascal_cell(row, col)
return 1 if col == 0 or col == row
return pascal_cell(row - 1, col) + pascal_cell(row - 1, col - 1)
end
def pascal_array(depth)
Array.new(depth) {|i| Array.new(i + 1){|j| pascal_cell(i, j)}}
end
class Array
def to_centered_spaced_string
cell_width = self.flatten.collect{|val| val.to_s.size}.max + 2
row_width = self.collect{|row| row.size}.max * cell_width
rows = Array.new(self.size) do |i|
self[i].collect{|item|item.to_s.center(cell_width)}.join.center(row_width)
end
return rows.join("\n")
end
end
puts pascal_array(ARGV[0].to_i).to_centered_spaced_string