I have a list of integers from 1 - 400. I choose n
random numbers with replacement from the list.
Here is an example:
list =
{5, 14, 15, 29, 38, 100, 112, 139, 156, 165, 195, 217,
228, 237, 249, 286, 320, 323, 325, 329, 335, 349}
If I have this list, the routine should find all the sublists with all the elements within 60 of each other
{{5, 14, 15, 29, 38}, {14, 15, 29, 38}, {15, 29, 38}, {29, 38},
{100, 112, 139, 156}, {112, 139, 156, 165}, {139, 156, 165, 195},
{156, 165, 195}, {165, 195, 217}, {195, 217, 228, 237, 249},
{217, 228, 237, 249}, {228, 237, 249, 286}, {237, 249, 286}, {249, 286},
{286, 320, 323, 325, 329, 335}, {320, 323, 325, 329, 335, 349},
{323, 325, 329, 335, 349}, {325, 329, 335, 349}, {329, 335, 349}, {335, 349}}
list
, in this case is sorted but that will generally not be true. I have a small program working now but it is less general and very slow. Can something be done here?
Here is my code:
between[l_] := Module[{},
h = TakeWhile[l, # <= l[[1]] + 60 &];
s = Drop[s, 1];
h
]
s = Sort[RandomChoice[Range[400], 22]];
Table[between[s], {Length[s] - 7 + 1}]
Split
andGather
are useful functions for this type of problem. $\endgroup$