Using Ruby 2.4, I have an array of unique, ordered numbers, for example
[1, 7, 8, 12, 14, 15]
How do I find the first two elements whose differ
One way to do this:
a.each_with_index { |e, i| break [ e, a[i.next] ] if a[i.next] == e.next }
#=> [7, 8]
Unlike chunk or each_cons this doesn't create an array of arrays. It also breaks as soon as a pair is found.
require 'fruity'
arr = ((1...1000)).to_a.reverse + [1,2]
def first_adjacent_pair(arr)
idx = arr.each_index.drop(1).find { |i| (arr[i-1]-arr[i]).abs == 1 }
idx ? arr[idx-1, 2] : nil
end
def first_adjacent_pair2(arr)
enum = arr.to_enum
loop do
curr = enum.next
nxt = enum.peek
return [curr, nxt] if (curr-nxt).abs == 1
end
nil
end
compare do
iceツ { ar = arr.dup; ar.each_with_index { |e, i| break [ e, ar[i.next] ] if ar[i.next] == e.next } }
cary { ar = arr.dup; first_adjacent_pair(ar) }
cary2 { ar = arr.dup; first_adjacent_pair2(ar) }
seb { ar = arr.dup; ar.each_cons(2).find{|a,b| b-a == 1} }
end
#Running each test 64 times. Test will take about 1 second.
#cary2 is faster than cary by 3x ± 0.1
#cary is faster than iceツ by 3x ± 0.1 (results differ: [999, 998] vs [1, 2])
#iceツ is faster than seb by 30.000000000000004% ± 10.0%