Skip to content

Latest commit

 

History

History
22 lines (17 loc) · 524 Bytes

enumerable_magic_№20_cascading_subsets.md

File metadata and controls

22 lines (17 loc) · 524 Bytes

Description

Create a method each_cons that accepts a list and a number n, and returns cascading subsets of the list of size n, like so:

each_cons([1,2,3,4], 2)
#=> [[1,2], [2,3], [3,4]]

each_cons([1,2,3,4], 3)
#=> [[1,2,3],[2,3,4]]

As you can see, the lists are cascading; ie, they overlap, but never out of order.

My Solution

def each_cons(list, n)
  list.each_cons(n).to_a
end