--Apple-Mail-1--1004602757
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	delsp=yes;
	format=flowed

On Jun 23, 2006, at 8:31 AM, Ruby Quiz wrote:

> by Dirk Meijer
>
> I recently showed a friend what an amazing language Ruby was, by  
> quickly
> programming up a script to calculate Fibonacci's Sequence, and his  
> first
> response was: "Can you do Pascal's Triangle?"  So I did, which  
> proved harder
> than expected.

My own solution, used in the quiz examples.

James Edward Gray II


--Apple-Mail-1--1004602757
Content-Transfer-Encoding: 7bit
Content-Type: text/x-ruby-script;
	x-unix-mode=0644;
	name="pp_pascal.rb"
Content-Disposition: attachment;
	filename=pp_pascal.rb

#!/usr/local/bin/ruby -w

rows     = (ARGV.shift || 10).to_i
triangle = Array.new

rows.times do |row|
  case row
  when 0
    triangle << [1]
  when 1
    triangle << [1, 1]
  else
    triangle << [1]
    (row - 1).times do |i|
      triangle[-1] << triangle[-2][i] + triangle[-2][i + 1]
    end
    triangle[-1] << 1
  end
end

exit if rows.zero?

field_width = triangle[-1].map { |n| n.to_s.size }.max
triangle.map! do |row|
  row.map { |n| n.to_s.center(field_width) }.join(" " * field_width)
end

row_width = triangle[-1].size
triangle.each do |row|
  puts row.center(row_width)
end

--Apple-Mail-1--1004602757--