Given a list say
{"a", "b", "c", "d"}
Is there any easier way to generate list of sequential subsets like this (order of the result is not important)
{
{"a"},
{"a b"},
{"a b c"},
{"a b c d"},
{"b"},
{"b c"},
{"b c d"},
{"c"},
{"c d"},
{"d"}
}
I think I like this best of all:
set = {"a", "b", "c", "d"};
ReplaceList[set, {___, x__, ___} :> {x}]
With the string joining:
ReplaceList[set, {___, x__, ___} :> "" <> Riffle[{x}, " "]]
In a similar vein, specific to strings:
StringCases["abcd", __, Overlaps -> All]
Since Nasser says I am cheating, here is a more manual approach that also has greater efficiency on large sets:
ClearAll[f, f2]
f[i_][x_] := NestList[i, x, Length@x - 1]
f2[set_] := Join @@ ( f[Most] /@ f[Rest][set] )
f2[{"a", "b", "c", "d"}]
Flatten[Partition[{a, b, c, d}, #, 1] & /@ {1, 2, 3, 4}, 1]
gives
{{a}, {b}, {c}, {d}, {a, b}, {b, c}, {c, d}, {a, b, c}, {b, c, d}, {a, b, c, d}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With