Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
次のような整数の配列が与えられます。
array = [1,2,3,5,6,7,20,21,33,87,88,89,101]
この配列には、「1,2,3」や「87,88,89」などのk連続したサブシーケンス (この場合は) が含まれています。k = 6これらのサブシーケンスの配列を含む配列を取得するにはどうすればよいkですか?
k
k = 6
上記の例では、これらは次のようになります。
[[1,2,3], [5,6,7], [20,21], [33], [87,88,89], [101]]
シフトされていない0のはダミーです。任意の数になる可能性があります。
0
[1, 2, 3, 5, 6, 7] .unshift(0) .each_cons(2).slice_before{|m, n| m + 1 < n}.map{|a| a.map(&:last)} # => [[1, 2, 3], [5, 6, 7]]