Ruby group non-zero numbers and sequential times

73 views Asked by At

I have an array of items like so: [DateTime, value]. I want to group the items which satisfy the following conditions:

  • A sequence of 3 or more items
  • Items contain values which are > 0
  • times are sequential (increasing by 1 second)

I want to get the indices of the start and end of the sequences that meet those conditions.

E.g.

[ 
[today 10:00:00, 1],
[today 10:00:01, 1],
[today 10:00:02, 1],
[today 10:00:03, 0],
[today 10:00:04, 1],
[today 10:00:05, 1],
[today 10:00:16, 1],
[today 10:00:17, 1],
]

Should return:

[ [0,2] ]

I had a look at Daru and NMatrix but I couldn't figure out how to do sequential comparisons to do what I want.

Now I've just got a big loop that does a lot of comparisons - is there a better way to do it?

2

There are 2 answers

2
sawa On BEST ANSWER
t = Time.now
a = [
  [t + 0, 1],
  [t + 1, 1],
  [t + 2, 1],
  [t + 3, 0],
  [t + 4, 1],
  [t + 5, 1],
  [t + 16, 1],
  [t + 17, 1],
]

a
.each_with_index
.chunk_while{
  |(x, i), (y, j)|
  x[1].positive? and
  y[1].positive? and
  x[0].to_i.next == y[0].to_i
}
.select{|chunk| chunk.length >= 3}
.map{|chunk| [chunk.first[1], chunk.last[1]]}
# => [[0, 2]]
1
Cary Swoveland On
t = Time.now
arr = [[t+ 0, 1], [t+ 1, 1], [t+ 2, 1], [t+ 3, 0], [t+ 4, 1], [t+ 5, 1], [t+16, 1],
       [t+18, 1], [t+19, 1], [t+20, 1], [t+21, 1], [t+30, 1]]
  #=> [[2018-11-06 10:11:52 -0800, 1], [2018-11-06 10:11:53 -0800, 1],...,
  #    [2018-11-06 10:12:22 -0800, 1]]

arr.each_index.
    slice_when { |i,j| arr[i].last.zero? || arr[i-1].last.zero? ||
      (arr[i].first - arr[i-1].first > 1) }.
    each_with_object([]) { |a,b| b << [a.first, a.last] if a.last-a.first >= 2 }
  #=> [[0, 2], [7, 10]]

Emumerable#slice_when (new in MRI v2.2) is closely related to Enumerable#chunk_while (new in MRI v2.3), which @sawa used in his answer. Generally, if one can be used, the other is an alternative.

To support earlier versions of Ruby one could use Enumerable#slice_before (new in MRI v1.9.2).

arr.each_index.
    slice_before { |i| i > 0 &&
      (arr[i].last.zero? || arr[i-1].last.zero? || (arr[i].first - arr[i-1].first > 1)) }.
    each_with_object([]) { |a,b| b << [a.first, a.last] if a.last-a.first >= 2 }

Note the intermediate calculation:

enum = arr.each_index.slice_before {|i| i > 0 &&
  (arr[i].last.zero? || arr[i-1].last.zero? || (arr[i].first - arr[i-1].first > 1)) }
  #=> => #<Enumerator: #<Enumerator::Generator:0x0000000001948d50>:each>
enum.to_a
  #=> [[0, 1, 2], [3], [4, 5], [6], [7, 8, 9, 10], [11]]

(The return value is the same when using slice_when.) If preferred, the clause

each_with_object([]) { |a,b| b << [a.first, a.last] if a.last-a.first >= 2 }

could be replaced with a two-pass alternative:

select { |a| a.last - a.first >= 2 }.map { |a| [a.first, a.last] }