How do I find the first two consecutive elements in my array of numbers?

前端 未结 5 836
长发绾君心
长发绾君心 2021-01-25 06:06

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

5条回答
  •  我在风中等你
    2021-01-25 06:49

    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.


    Benchmarks

    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%
    

提交回复
热议问题