On Mon, 27 Nov 2000, Brian F. Feldman wrote:

> The final, fastest version I know of is:
> 
> class Array
> 
>   def interleave(ary2)
>     a1, a2, res = self.reverse, ary2.reverse, []
>     res.push a1.pop, a2.pop until a1.empty? or a2.empty?
>     for n in [a1, a2]
>       res.concat n unless n.empty?
>     end
>     res
>   end
> 
> end

Except... you haven't (un)reversed what's left of a1 and a2.  So, for
example:

a1 = [1, 2, 3, 4]
a2 = %w{ one two }

p a1.interleave(a2) # => [1, "one", 2, "two", 4, 3]

So:

   res.concat n.reverse unless n.empty?

though dropping the whole 'for' construct and replacing the final
'res' with this:

   res + (a1 + a2).reverse

(snagging the '+' instead of concat approach from Dave's iterator
version) runs a little faster.  To save some space (I assume, since +
creates a new array):

   res.concat (a1 + a2).reverse

And other permutations, yet unborn.... :-)


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav