Im a trying to split a sorted 1D list of integers by sets of consecutives integers. The keywords in the question didn't trigger the answers I was looking for so I ask the question, no matter how trivial it looks like...
I just want for instance transform a list like:
pts= {915, 916, 917, 1324, 1325, 1326, 2070, 2071, 2072, 2073, 2817, 2818, 2819, 2820, 3568, 3569, 3570}
Into a list like:
{{915, 916, 917}, {1324, 1325, 1326}, {2070, 2071, 2072, 2073}, {2817,
2818, 2819, 2820}, {3568, 3569, 3570}}
I tried FindClusters[ ] but could not tune the classification function fine enough and kept finding some cases where sets differing by a small difference where grouped together. I tried also similar global operators Gather[]but I always found some specific cases where sets were not correct.
So, swallowing my pride, I went to a procedural program:
clusterize[pts_] := Module[{j = 1, c = {}, b},
While[j <= Length[pts],
b = {pts[[j]]};
Do[If[pts[[i + 1]] - pts[[i]] == 1, AppendTo[b, pts[[i + 1]]],
j = i; Break[]], {i, j, Length[pts] - 1}]; AppendTo[c, b];
j = j + 1;]; Return[c]]
But this is not satisfactory, I keep finding idiotic cases like:
pts= {915, 916, 917, 1324, 1325, 1326, 2070, 2071, 2072, 2073, 2817, 2818,2819, 2820, 3568, 3569, 3570}
Gives:
In[27]:= clusterize[pts]
Out[27]= {{915, 916, 917}, {1324, 1325, 1326}, {2070, 2071, 2072,
2073}, {2817, 2818, 2819, 2820}, {3568, 3569, 3570}, {3569,
3570}, {3570}}
So can someone help me to find the bug in my procedural programming or much better a functionnal programming way to do the trick (or with patterns) ?
FindClusters[]
does work. $\endgroup$