Hi,

I do like your solution, though I didn't know lambda nor zip before.
Cool way to build the new row!

Oh, and you wrote :

width   = biggest.to_s.length
width   +=1     if width % 2 == 0

which is actually :
width   = biggest.to_s.length|1

At first, I used :
width   = biggest.to_s.length/2*2+1
but tried to find an easier way to increment to the next odd number!

Bye,

Eric






Le Dimanche 25 Juin 2006 19:52, Erik Veenstra a crit:
> Here's an explanation about how to calculate the next row,
> based on the previous row. You only need the last line.
>
>  row                                    ===> [1, 3, 3, 1]
>  [0]+row                                ===> [0, 1, 3, 3, 1]
>  row+[0]                                ===> [1, 3, 3, 1, 0]
>  ([0]+row).zip(row+[0])                 ===> [[0, 1], [1, 3], [3, 3],
> [3, 1], [1, 0]]
>  ([0]+row).zip(row+[0]).map{|a,b|a+b}   ===> [1, 4, 6, 4, 1]
>
> You can replace the last line by the next line, but this one
> modifies the previous row, which might be tricky. Though it
> does save you one extra byte ;]
>
>  ([0]+row).zip(row<<0).map{|a,b|a+b}    ===> [1, 4, 6, 4, 1]
>
> You could build and store the complete triangle with this:
>
>  t=[r=[1]]+(2..n).map{r=([0]+r).zip(r+[0]).map{|a,b|a+b}}
>
> (But that's not what we are going to do in this script....)
>
> I added pretty printing as well. First of all, the width of
> each cell (excluding the separator) shouldn't be even, in order
> to be able to align properly.
>
> Secondly, if we can't center the contents within a cell, we
> error to the right on the left hand side of the triangle and to
> the left at the right hand side.
>
> Thirdly, because the last line could be stripped by half a cell
> width at both the left and the right hand side, we simply strip
> every line by half a cell width on both sides.
>
> You can find the source in the attachment.
>
> gegroet,
> Erik V. - http://www.erikveen.dds.nl/