I have a list $L$ of Length
$n\geq6$ and a function $f$ that, when applied to $L$, generates $n-5$ lists $\mathscr{L}_i$ of Length $n-1$ (at the outermost level). I want to then apply $f$ to each $\mathscr{L}_i$, then repeat with the generated lists and keep doing that until I get $(n-5)!$ lists of length 5 (which I suppose is right).
I only wanna do this for small $n$, so performance is not a problem, I just can't figure out the way to do it: I've been trying Nest
, NestWhile
and the List versions together with Table
to access the generated list elements but to no success.
To show you exactly what I'm doing, for $n=8$, I have e.g.
f[Range[8]]
Flatten[Table[f[%[[p]]], {p, Length[%]}], 1]
Flatten[Table[f[%[[p]]], {p, Length[%]}], 1]
for which the output is
{{1, 2, {3, 4}, 5, 6, 7, 8},
{1, 2, 3, {4, 5}, 6, 7, 8},
{1, 2, 3, 4, {5, 6}, 7, 8}}
{{1, 2, {{3, 4}, 5}, 6, 7, 8},
{1, 2, {3, 4}, {5, 6}, 7, 8},
{1, 2, {3, {4, 5}}, 6, 7, 8},
{1, 2, 3, {{4, 5}, 6}, 7, 8},
{1, 2, {3, 4}, {5, 6}, 7, 8},
{1, 2, 3, {4, {5, 6}}, 7, 8}}
{{1, 2, {{{3, 4}, 5}, 6}, 7, 8},
{1, 2, {{3, 4}, {5, 6}}, 7, 8},
{1, 2, {{3, {4, 5}}, 6}, 7, 8},
{1, 2, {3, {{4, 5}, 6}}, 7, 8},
{1, 2, {{3, 4}, {5, 6}}, 7, 8},
{1, 2, {3, {4, {5, 6}}}, 7, 8}}
(it doesn't really matter what *) however, when I try to do this with a one-liner I don't get what I expect,f
does
NestWhileList[
Flatten[Table[f[# &[[p]]], {p, Length[# &]}], 1],
f[Range[8]], Length[#] & < 3!]
just gives
{{{1, 2, {3, 4}, 5, 6, 7, 8},
{1, 2, 3, {4, 5}, 6, 7, 8},
{1, 2, 3, 4, {5, 6}, 7, 8}}}
*
I didn't think the particular definition of f
would matter, but since it is mentioned in the comments, this is it:
f[L_] := Table[
Delete[ReplacePart[L, p+1 -> {L[[p+1]], L[[p+2]]}], p+2]
, {p, 2, Length[L]-4}] /; Length[L] >= 6
&
seems to be in awkward places. TryNestWhileList[Flatten[Table[f[#[[p]]], {p, Length[#]}], 1] &, f[Range[8]], Length[#] & < 3!]
. $\endgroup$f
does and then supply an example wheref
produces an output that matches the description in the first paragraph. Really a crime not to supplyf
in this question. $\endgroup$f
. $\endgroup$