[QUIZ][SOLUTION] pp Pascal (#84)

Hello,

you can take this as negative example, cause I store the triangle and
the formatted output :slight_smile: Thus it's much slower than most solutions.

kind regards
Robert

pascal_triangle.rb (1.15 KB)

Here is my solution...

rows = ( ARGV.shift || 1 ).to_i
tri = Array.new( rows ) { |i| Array.new( i+1 ) }
tri.each_with_index do |row, rowno|
    row.fill do |colno|
        ( colno == 0 or colno == rowno ) ? 1 : tri[rowno-1][colno-1] +
tri[rowno-1][colno]
    end
end

cols, col_w = tri.last.length, tri.last.max.to_s.length+1
tri.each do |row|
    row_strs = row.collect { |col| col.to_s.center( col_w ) }
    puts row_strs.join.center( cols*col_w )
end

mark

···

On 6/25/06, Robert Retzbach <rretzbach@googlemail.com> wrote:

Hello,

you can take this as negative example, cause I store the triangle and
the formatted output :slight_smile: Thus it's much slower than most solutions.

kind regards
Robert

#!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

--
Mark Van Holstyn
mvette13@gmail.com
http://lotswholetime.com