Begin forwarded message: > From: "John Baylor" <john.baylor / gmail.com> > Date: June 27, 2006 11:55:52 PM CDT > To: submission / rubyquiz.com > Subject: Please Forward: Ruby Quiz Submission > > This is my first hack at a quiz solution - the previous ones have > been fun to read and follow; I'm looking forward to the next one > (where I won't be quite so late in my answer). > > # pp_pascal.rb > # for details, see http://www.rubyquiz.com/quiz84.html > > class String > def center( width ) > return self if self.length >= width > ret = self > 0.upto(width) do |w| > ret = ret + ' ' > return ret if ret.length >= width > ret = ' ' + ret > return ret if ret.length >= width > end > ret > end > end > > class Integer > def to_centered_s( width ) > ret = (self > 0) ? self.to_s.center(width) : '' > end > end > > > rows = ARGV[0].to_i > max = 0 > allRows = [] > prevRow = [0,1,0] > allRows[0] = prevRow > (2..rows).each do |row| > nextRow = [] > (2..prevRow.length).each do |col| > nextVal = (prevRow[col-2]+prevRow[col-1]) > nextRow << nextVal > end > prevRow = [0, nextRow, 0].flatten > allRows[row-1] = prevRow > end > maxWidth = prevRow[( prevRow.length / 2).to_i].to_s.length > totalWidth = rows * (maxWidth*2) > puts "rows=#{rows}, mw=#{maxWidth}, tw=#{totalWidth}" > allRows.each do |row| > row = row[1..-2] > row.collect! { |x| x = x.to_centered_s (maxWidth*2-1) } > puts row.join(' ').center(totalWidth) > end > > > Now I can look at the other solutions... > > thanks! > JB >