数组交错合并

class Array
  def interlace(array)
    self.empty?? array : [self[0]] + array.interlace(self[1..-1])
  end
end

 

具体示例:

 

    p [1, 2, 3, 4, 5, 6].interlace([7, 8, 9]) # => [1, 7, 2, 8, 3, 9, 4, 5, 6]
    p [1, 2, 3].interlace([1, 2, 3, 4, 5])    # => [1, 1, 2, 2, 3, 3, 4, 5]

 

你可能感兴趣的:(数组)