From: Robert Retzbach Date: 2006-06-26T01:58:50+09:00 Subject: [QUIZ][SOLUTION] pp Pascal (#84) --------------010107070200090108040004 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit Hello, you can take this as negative example, cause I store the triangle and the formatted output :) Thus it's much slower than most solutions. kind regards Robert --------------010107070200090108040004 Content-Type: text/plain; name="pascal_triangle.rb" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="pascal_triangle.rb" #!C:\ruby\bin\ruby.exe class PTriangle def build num = 4 @body = Array.new # fill the empty triangle # using non-recursive algo ( num + 1 ).times do | row | row.times do | pos | if pos == 0 or pos == row - 1 number = 1 else number = @body[ row - 2 ][ pos - 1 ] + @body[ row - 2 ][ pos ] end @body[ row - 1 ] ||= [] @body[ row - 1 ][ pos ] = number end end @body end def render @rendered = [] # calculate maximum number length # needed for alignment max = @body.last[ @body.last.size / 2 ].to_s.size # shift all lines to the right if a new line is added # right justify all numbers @body.each do | row | @rendered.map! { | line | ' ' * max << line } @rendered << row.map { | num | num.to_s.rjust( max * 2, ' ' ) }.join end # cleanup, don't know how to do it more elegant # => remove not necessary indentation @rendered.map! { | line | line[ ( max * 2 - 1 )..-1 ] } end end if __FILE__ == $0 level = ARGV.first.to_i level = 4 unless level > 0 pt = PTriangle.new pt.build level puts pt.render end --------------010107070200090108040004--