5
$\begingroup$

I'm looking for a function which does the following:

ListSequenceSwap[{6, {1, 2, 3}, 4, 5, {2, 2}, {4}}]

{{6}, 1, 2, 3, {4, 5}, 2, 2, 4}

ListSequenceSwap[{6, {0}}]

{{6}, 0}

ListSequenceSwap[{6, {0}, {1}}]

{{6}, 0, 1}

ListSequenceSwap[{6, 7, {0}, {1}}]

{{6, 7}, 0, 1}

The solution I found is so convoluted that I would prefer not to show it.

$\endgroup$

6 Answers 6

2
$\begingroup$
Clear["Global`*"];

testcases = {
  {6, {1, 2, 3}, 4, 5, {2, 2}, {4}}
  , {6, {0}}
  , {6, {0}, {1}}
  , {6, 7, {0}, {1}}
  }

SequenceReplace[#, {{a__?NumericQ} :> {a}, {x_List} :> 
     Sequence @@ x }] & /@ testcases

{{{6}, 1, 2, 3, {4, 5}, 2, 2, 4}, {{6}, 0}, {{6}, 0, 1}, {{6, 7}, 0,
1}}

$\endgroup$
4
$\begingroup$
lsSwap = Replace[SplitBy[#, ListQ], a : {__List} :> Splice@Flatten[a], {1}]&

Examples:

lsSwap[{6, {1, 2, 3}, 4, 5, {2, 2}, {4}}]
{{6}, 1, 2, 3, {4, 5}, 2, 2, 4}
lsSwap[{6, {0}}]
{{6}, 0}
lsSwap[{6, {0}, {1}}]
{{6}, 0, 1}
lsSwap[{6, 7, {0}, {1}}]
{{6, 7}, 0, 1}
$\endgroup$
2
$\begingroup$
spliceDeepLists[a : {__List}] := Splice@Flatten@a
spliceDeepLists[a_] := a;

swapLS = Map[spliceDeepLists]@SplitBy[#, ListQ] &;

Examples:

swapLS[{6, {1, 2, 3}, 4, 5, {2, 2}, {4}}]
{{6}, 1, 2, 3, {4, 5}, 2, 2, 4}
swapLS[{6, {0}}]
{{6}, 0}
swapLS[{6, {0}, {1}}]
{{6}, 0, 1}
swapLS[{6, 7, {0}, {1}}]
{{6, 7}, 0, 1}
$\endgroup$
2
$\begingroup$
step[{a___}, b_List] := {a, Splice @ b}
step[{a___, b_List}, c_] := {a, Append[b, c]}
step[{a___}, b_] := {a, {b}}

sequenceListSwap = Fold[step, {}, #] &;

Examples:

sequenceListSwap @{6, {1, 2, 3}, 4, 5, {2, 2}, {4}}
{{6}, 1, 2, 3, {4, 5}, 2, 2, 4}
sequenceListSwap @ {6, {0}}
{{6}, 0}
sequenceListSwap @ {6, {0}, {1}}
{{6}, 0, 1}
sequenceListSwap @ {6, 7, {0}, {1}}
{{6, 7}, 0, 1}
$\endgroup$
1
  • $\begingroup$ (+1) Nice way to use Fold! $\endgroup$ Commented Oct 1, 2023 at 23:14
1
$\begingroup$
listSequenceSwap = SequenceReplace[a : {__Splice} :> (# & @@@ a)] @* Map[Splice];

Examples:

listSequenceSwap @ {6, {1, 2, 3}, 4, 5, {2, 2}, {4}}
{{6}, 1, 2, 3, {4, 5}, 2, 2, 4}
listSequenceSwap @ {6, {0}}
{{6}, 0}
listSequenceSwap @ {6, {0}, {1}}
{{6}, 0, 1}
listSequenceSwap @ {6, 7, {0}, {1}} 
{{6, 7}, 0, 1}
$\endgroup$
1
$\begingroup$

Using SplitBy and Cases:

lst = {6, 7, {0}, {1}, 4, 5, 8, {2, 2}, {4}};
Cases[SplitBy[lst, ListQ], x_ :> If[VectorQ[x], x, Splice@Flatten@x]]

(*{{6, 7}, 0, 1, {4, 5, 8}, 2, 2, 4}*)

Also, in a more programmatic way:

 ListSequenceSwap[list_List] := 
 Module[{element, flist, len, i, temp}, flist = {};
 len = Length[list];
 i = 1;
 While[i <= len, element = list[[i]];
 If[NumericQ[element], temp = {element};
 While[i < len && NumericQ[list[[i + 1]]], 
 AppendTo[temp, list[[i + 1]]]; i++];
 AppendTo[flist, temp]; i++, 
 If[Head[element] === List, flist = Join[flist, element], 
 AppendTo[flist, {element}]]; i++]]; flist]

Testing ListSequenceSwap:

ListSequenceSwap[lst]

(*{{6, 7}, 0, 1, {4, 5, 8}, 2, 2, 4}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.