------ art_12540_18833699.1151272291096
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Here is my solution...
rows ARGV.shift || 1 ).to_i
tri rray.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 ri.last.length, tri.last.max.to_s.length+1
tri.each do |row|
row_strs ow.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 :) Thus it's much slower than most solutions.
>
> kind regards
> Robert
>
>
>
> #!C:\ruby\bin\ruby.exe
>
> class PTriangle
> def build num
> @body rray.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
> else
> number body[ row - 2 ][ pos - 1 ] + @body[ row - 2 ][ pos ]
> end
> @body[ row - 1 ] || ]
> @body[ row - 1 ][ pos ] umber
> 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 RGV.first.to_i
> level unless level > 0
>
> pt Triangle.new
> pt.build level
> puts pt.render
> end
>
>
>
--
Mark Van Holstyn
mvette13 / gmail.com
http://lotswholetime.com
------ art_12540_18833699.1151272291096--