如何将整数数组汇总为范围数组?

Lar*_*nal 9 ruby arrays algorithm functional-programming range

我想接受以下输入:

[1,2,4,5,6,7,9,13]
Run Code Online (Sandbox Code Playgroud)

把它变成如下的东西:

[[1,2],[4,7],[9,9],[13,13]]
Run Code Online (Sandbox Code Playgroud)

每个子数组代表一个整数范围.

tok*_*and 21

使用Enumerable#chunk的功能方法:

ranges = [1, 2, 4, 5, 6, 7, 9, 13]
  .enum_for(:chunk) # .chunk for Ruby >= 2.4
  .with_index { |x, idx| x - idx }
  .map { |_diff, group| [group.first, group.last] }

#=> [[1, 2], [4, 7], [9, 9], [13, 13]]
Run Code Online (Sandbox Code Playgroud)

它是如何工作的:一旦索引,数组中的连续元素具有相同的x - idx,所以我们使用该值来对输入数组进行块(连续项的分组).最后,我们只需要采用每个组的第一个和最后一个元素来构建对.

  • 再采取一步,`.map {| min,max | min == max?min:min .. max}`将导致:`[1..2,4..7,9,13]`. (2认同)
  • 在Ruby 2.4中,`enum_for`将[不再需要](https://github.com/ruby/ruby/commit/69846644d25b0a40ab787b150716a5d17e63f6a8). (2认同)

ste*_*lag 5

这几乎是来自可枚举的#lif_before方法文档:

ar = [1,2,4,5,6,7,9,13]
prev = ar[0]
ar.slice_before{|e|prev,prev2 = e,prev; prev2.succ != e}.map{|a|a.first..a.last}
#=> [1..2, 4..7, 9..9, 13..13]
Run Code Online (Sandbox Code Playgroud)

这应该适用于字符,日期,任何.succ方法.


Mar*_*une 5

一个比@tokland 非常好的解决方案更简单的解决方案是使用chunk_while

xs.chunk_while { |a, b| a + 1 == b }.map do |seq|
  [seq.first, seq.last]
end
Run Code Online (Sandbox Code Playgroud)

注意chunk_while在 Ruby 2.3 中引入