0
$\begingroup$

I have a list of the form

SpecialCycle12={{{1, 2, 0, 0, 0, 0, 0}}, {{3, 2, 1, 0, 0, 0, 0}}}`

And the list

primitiveNonSpecialCycleCase2 = {
  {{3}, {4}, {5}, {6}, {7}}, {{3, 4}, {5}, {6}, {7}},
  {{3, 4}, {5, 6}, {7}}, {{3, 4, 5}, {6}, {7}},
  {{3, 4, 5}, {6, 7}}, {{3, 4, 5, 6}, {7}}, {{3, 4, 5, 6, 7}}
 }

I want to replace the elements in the smallest parenthesis of List2 in the position represented by the previous element in the elements of List1. For instance, the first element of List1 has 5 zeroes and so I will replace all the elements of List2 that have 5 numbers, first I want to consider {{3}, {4}, {5}, {6}, {7}} and I will replace 3 in position 3, 4 in position 4 and so on and finally want to get {1,2,3,4,5,6,7}; when I consider {{3, 4}, {5}, {6}, {7}} I want to replace 4 in position 3, 3 in position 4, 5 in position 5, 6 in position 6 and so on to finally get {1,2,4,3,5,6,7}; when I consider {{3, 4}, {5, 6}, {7}} I want to replace 3 in position 4, 4 in position 3, 5 in position 6, 6 in position 5 and finally 7 in position 7, to finally get {1,2,4,3,6,5,7}.

I tried doing this with the following line of code

Table[
  ReplacePart[SpecialCycle12[[i]][[l]],
    Thread[RotateRight[primitiveNonSpecialCycleCase2[[i]][[j]][[k]]] -> primitiveNonSpecialCycleCase2[[i]][[j]][[k]]]
   ],
  {i, Length[SpecialCycle12]}, {j, Length[primitiveNonSpecialCycleCase2]},
  {k, Length[primitiveNonSpecialCycleCase2[[j]]]}, {l, Length[SpecialCycle12[[i]]]}
 ]

But I am getting a result of the form

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

Does anyone have a suggestion on how I could improve my code to get the result I want?

$\endgroup$

closed as unclear what you're asking by ciao, user9660, m_goldberg, ubpdqn, Öskå Jun 29 '16 at 17:00

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Young Jun 26 '16 at 15:52
  • $\begingroup$ Note that indentation is the preferred use of posting code that's not inline. $\endgroup$ – Feyre Jun 26 '16 at 15:56
  • $\begingroup$ So, do I get it right that when the depth of the source list is 4, you want to start filling in from the rear? What do you want to do with the sublist that starts {3,2,1,...? $\endgroup$ – Feyre Jun 26 '16 at 16:00
  • $\begingroup$ What do you mean with the rear? With the sublist {3,2,1,0,0,0,0} I want to do the same kind of replacement, but using the elements of primitiveNonSpecialCycleCase2 that have only 3 elements. $\endgroup$ – Cesar Jun 26 '16 at 16:05
  • $\begingroup$ As in, start at the last index of that sublist, as in with {3,4}, 4 goes first, and if it were {3,4,5,6}, 6 would go first. I assume you want a generic code that will work on all possible lists. $\endgroup$ – Feyre Jun 26 '16 at 16:08
1
$\begingroup$

Well, I thought this would be quite easy with tables, but the code turned into arguably a bit of a whale, still posting it in case you find it useful, after all it does work.

sc12 = {{{1, 2, 0, 0, 0, 0, 0}}, {{3, 2, 1, 0, 0, 0, 0}}};
pnscc2 = {{{3}, {4}, {5}, {6}, {7}}, {{3, 4}, {5}, {6}, {7}}, {{3, 
     4}, {5, 6}, {7}}, {{3, 4, 5}, {6}, {7}}, {{3, 4, 5}, {6, 
     7}}, {{3, 4, 5, 6}, {7}}, {{3, 4, 5, 6, 8}}};
Table[Table[
   If[Length[pnscc2[[i, j]]] != 1, 
    pnscc2[[i, j]] = Reverse[pnscc2[[i, j]]]], {j, 
    Length[pnscc2[[i]]]}]; 
  pnscc2[[i]] = Flatten[pnscc2[[i]]], {i, Length[pnscc2]}];
Table[If[Depth[pnscc2[[i, j]]] == 1, 
   pnscc2[[i, j]] = List[pnscc2[[i, j]]]], {i, Length[pnscc2]}, {j, 
   5}];
k = 1;
newlist = ConstantArray[sc12, Length[pnscc2]];
While[k < Length[pnscc2] + 1, j = 1; 
 Table[If[newlist[[k]][[1, 1, i]] == 0, 
   newlist[[k]] = 
    ReplacePart[newlist[[k]], {{1, 1, i}} -> pnscc2[[k, j, 1]]]; 
   j++], {i, 7}]; k++]
newlist
{{{{1, 2, 3, 4, 5, 6, 7}}, {{3, 2, 1, 0, 0, 0, 0}}},
{{{1, 2, 4, 3, 5, 6, 7}}, {{3, 2, 1, 0, 0, 0, 0}}},
{{{1, 2, 4, 3, 6, 5, 7}}, {{3, 2, 1, 0, 0, 0, 0}}},
{{{1, 2, 5, 4, 3, 6, 7}}, {{3, 2, 1, 0, 0, 0, 0}}},
{{{1, 2, 5, 4, 3, 7, 6}}, {{3, 2, 1, 0, 0, 0, 0}}},
{{{1, 2, 6, 5, 4, 3, 7}}, {{3, 2, 1, 0, 0, 0, 0}}},
{{{1, 2, 8, 6, 5, 4, 3}}, {{3, 2, 1, 0, 0, 0, 0}}}}
$\endgroup$
0
$\begingroup$

How about this? As an example, let

lst = {1, 2, 0, 0, 0, 0, 0};
perm1 = {{3, 4}, {5}, {6}, {7}};
perm2 = {{3, 4, 5}, {6}, {7}};

Then, define

f = ReplacePart[#1, Thread[Position[#1, 0] -> Flatten@Position[#, 0]]][[PermutationList[Cycles@#2, Length@#1]]] &

and

f[lst, perm1]
(* {1, 2, 4, 3, 5, 6, 7} *)
f[lst, perm2]
(* {1, 2, 4, 5, 3, 6, 7} *)

From here, it's a matter of putting this function through a Table looping over the elements of your two lists.

$\endgroup$
  • $\begingroup$ This is not yet a complete answer, I think, because some things are unclear. (1) Are the elements of primitiveNonSpecialCycleCase2 meant to act on {3, 2, 1, 0, 0, 0, 0} as well (even though there are four zeroes in the list and the elements of primitiveNonSpecialCycleCase2 run 3 through 7)? (2) The first "non-trivial" permutation involves three elements, so what is the expected outcome for list = {1, 2, 0, 0, 0, 0, 0} and perm = {{3, 4, 5}, {6, 7}}. $\endgroup$ – march Jun 27 '16 at 2:38

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