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,所以我们使用该值来对输入数组进行块(连续项的分组).最后,我们只需要采用每个组的第一个和最后一个元素来构建对.
这几乎是来自可枚举的#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方法.
一个比@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 中引入