Function Repository Resource:

SequenceSelect

Source Notebook

Find sublists satisfying a criterion

Contributed by: Bob Sandheinrich

ResourceFunction["SequenceSelect"][{e1,e2,},crit]

gives the sublists in the list for which crit[{ei,ej,}] is True.

ResourceFunction["SequenceSelect"][{e1,e2,},crit,n]

picks out the first n results.

Details

With the default option setting OverlapsFalse, ResourceFunction["SequenceSelect"] includes only sublists that do not overlap. With the setting OverlapsTrue, it includes sublists that overlap.
With OverlapsAll multiple sublists that meet the criteria are all included. With OverlapsTrue, only the first such matching sublist at a given position is included.

Examples

Basic Examples (2) 

Find sequences in a list of integers that have a specific mean:

In[1]:=
ResourceFunction["SequenceSelect"][{1, 2, 3, 4, 5, 6, 7, 8}, Mean[#] == 11/2 &]
Out[1]=

Find sets of colors all significantly different than Red:

In[2]:=
ResourceFunction[
 "SequenceSelect"][{RGBColor[
  0.34079012808924714`, 0.6426413893861012, 0.6245067166882166], RGBColor[0.9856786658891481, 0.864731138236835, 0.2697060635901771],
   RGBColor[
  0.5781488531679828, 0.15163068229557886`, 0.3424060440097514], RGBColor[
  0.7087005067324104, 0.6607229011569486, 0.47912537489047446`], RGBColor[
  0.8987128457090641, 0.9943983943823145, 0.49447162386500376`], RGBColor[
  0.16413843127745453`, 0.5099958575379266, 0.7161053277114648], RGBColor[
  0.05670488715276267, 0.8941336555301387, 0.08515446380352953], RGBColor[
  0.6074683516094217, 0.005046807981735846, 0.7673776445641829], RGBColor[
  0.9508088739587199, 0.4181040199667614, 0.9465093313520183], RGBColor[
  0.26035678307673127`, 0.681458467741517, 0.2135868701840813]}, Length[#] > 0 && Min[ColorDistance[#, RGBColor[1, 0, 0]]] > 1 &]
Out[2]=

Scope (2) 

Find sublists of length three with a total over seven:

In[3]:=
ResourceFunction["SequenceSelect"][{1, 2, 3, 4, 5, 6, 7, 8}, Length[#] == 3 && Total@# > 7 &]
Out[3]=

Find up to five sublists of strings with a specific total length:

In[4]:=
ResourceFunction["SequenceSelect"][
 TextWords@ResourceData["Gettysburg Address"], StringLength[StringJoin[#]] == 50 &, 5]
Out[4]=

Options (3) 

By default the results do not include overlapping sublists:

In[5]:=
ResourceFunction["SequenceSelect"][Range[10], 4 <= Length[#] <= 5 && Total@# > 10 &]
Out[5]=

Use OverlapsTrue to include overlapping sublists:

In[6]:=
ResourceFunction["SequenceSelect"][Range[10], 4 <= Length[#] <= 5 && Total@# > 10 &, Overlaps -> True]
Out[6]=

Use OverlapsAll to include overlapping sublists and multiple sublists with the same starting element:

In[7]:=
ResourceFunction["SequenceSelect"][Range[10], 4 <= Length[#] <= 5 && Total@# > 10 &, Overlaps -> All]
Out[7]=

Properties and Relations (2) 

SequenceCases matches patterns:

In[8]:=
SequenceCases[Range[10], {_?PrimeQ, _, _?PrimeQ}]
Out[8]=

SequencePattern checks a test:

In[9]:=
ResourceFunction["SequenceSelect"][Range[10], Length[#] === 3 && PrimeQ[#[[1]]] && PrimeQ[#[[3]]] &]
Out[9]=

Version History

  • 1.0.0 – 28 September 2022

Related Resources

License Information