Bill Allombert on Fri, 11 May 2018 13:46:09 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Any method to find consecutive sequence in integer array?


On Fri, May 11, 2018 at 12:35:17PM +0530, chandra sekaran wrote:
> find_consecutive [1,2,3,5,6,9,10,2,3,4]
> => [[1, 2, 3], [5, 6], [9, 10], [2, 3, 4]]

If you really want to save on characters:

find_consecutive(V)=
{
  my(s=concat([0,[i|i<-[1..#V-1],V[i]+1!=V[i+1]],#V]));
  [V[s[i]+1..s[i+1]]|i<-[1..#s-1]]
}

Cheers,
Bill.