TransWikia.com

A list of factors of a binary word

Mathematica Asked by geoffrey on January 27, 2021

I want a list of ALL contiguous sub-sequences of a binary word w, say w={r,d,r,r,d}.

I have;

w = {r, d, r, r, r}; {Drop[w, 1], Drop[w, -1]} which gives the length 4 subsequences : {{d, r, r, d}, {r, d, r, r}}.

How do I get All of such sub-sequences?

That is I want: Input:={r,d,r,r,d}

Output:{ {r,d,r,r,d}, {d,r,r,d},{r,d,r,r},{r,r,d},{d,r,r},,{r,d,r},{r,d},{r,r},{d,r},{d},{r},{}}

(not necessarily in this order).

One Answer

You can use Subsequences:

list = {r, d, r, r, d};

DeleteDuplicates @ Subsequences @ list
{{}, {r}, {d}, {r, d}, {d, r}, {r, r}, {r, d, r}, {d, r, r}, {r, r, d}, 
 {r, d, r, r},  {d, r, r, d}, {r, d, r, r, d}}

You can also use SequenceCases with the option Overlaps -> All:

Union @ SequenceCases[list, {___}, Overlaps -> All] 
{{}, {d}, {r}, {d, r}, {r, d}, {r, r}, {d, r, r}, {r, d, r}, {r, r, d},
 {d, r, r, d}, {r, d, r, r}, {r, d, r, r, d}}

Correct answer by kglr on January 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP