On 26/06/2006, at 3:52 AM, Erik Veenstra wrote:

> 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]

For contrast, here's a way to do the same thing using enum_cons:

   row                                      ===> [1, 3, 3, 1]
   [0]+row+[0]                              ===> [0, 1, 3, 3, 1, 0]
   ([0]+row+[0]).enum_cons(2).map{|a,b|a+b} ===> [1, 4, 6, 4, 1]

A few more characters, but probably a little quicker to run given it  
doesn't have to construct so many intermediate arrays.

Pete Yandell
http://9cays.com/