Friday, 3 May 2019

Sequences of even numbers from an array in ruby

Finding the sequences of even numbers from an array in ruby


a = [1,2,3,4,444,6,5,6,7,8,44,55,333,555,66,88]
b = []
a.each_slice(2) do |l|
  if l && l[0] && l[0].even? && l[1] && l[1].even?
    b.push(l)
  end
  b
end
p b

output:  => [[444, 6], [66, 88]]


No comments:

Post a Comment

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...