7
$\begingroup$

Given a set of triple lists (all contain element 9!)

tripel = {{5, 10, 9}, {4, 9, 3}, {3, 9, 8}, {4, 5, 9}, {9, 10, 8}}

I would like to rearrange tripel to

Map[Which[#[[1]] == 9, #, #[[2]] ==9, {#[[2]], #[[3]], #[[1]]}, #[[3]] ==9, {#[[3]], #[[1]], #[[2]]}] &, tripel]
(*{{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {9, 10, 8}}*)

Is there a more direct and simple way to realize this cyclic permutation in Mathematica? Thanks!

$\endgroup$
2
  • $\begingroup$ Just checking something: should the outcome be {{9, 5, 10}, {9, 4, 3}, {9, 3, 8}, {9, 4, 5}, {9, 10, 8}} or is it correct as it is? $\endgroup$
    – bmf
    Nov 20 at 9:07
  • 1
    $\begingroup$ @bmf I think it's correct as it is $\endgroup$ Nov 20 at 9:13

6 Answers 6

6
$\begingroup$
triples = {{5, 10, 9}, {4, 9, 3}, {3, 9, 8}, {4, 5, 9}, {9, 10, 8}}

NestWhile[RotateLeft, #, First@# =!= 9 &] & /@ triples

{{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {9, 10, 8}}


EDIT

The solution above works if there are more entries per sublist, but will run in a loop if 9 is not present. To improve, additional arguments are needed:

triples2 = {{5, 10, 9}, {4, 9, 3}, {3, 9, 8}, {4, 5, 9}, {4, 10, 
   8}, {3, 6, 8, 9, 10}}

NestWhile[RotateLeft, #, First@# =!= 9 &, 1, Length@#] & /@ triples2

{{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {4, 10, 8}, {9, 10, 3, 6, 8}}

$\endgroup$
2
  • $\begingroup$ Thanks for your fast answer. It would be sufficient (in your edit) if only lists containing 9 would be returned! $\endgroup$ Nov 20 at 9:28
  • $\begingroup$ I don't know of a way to do it with NestWhile unless an If is used. But then, it would be easier to pre-filter sublists that don't have a 9. $\endgroup$
    – Syed
    Nov 20 at 9:31
8
$\begingroup$

Also using FirstPosition with the 3rd (default if no matching) parameter:

triples = {{5, 10, 9}, {4, 9, 3}, {3, 9, 8}, {4, 5, 9}, {9, 10, 8}, {1, 2, 3}};
RotateLeft[#, FirstPosition[#, 9, {1}] - 1] & /@ triples

(* {{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {9, 10, 8}, {1, 2, 3}} *)
$\endgroup$
2
  • 1
    $\begingroup$ can save 4 characters removing [[1]] (+1, of course) $\endgroup$
    – kglr
    Nov 20 at 10:16
  • $\begingroup$ @kglr Changed it as suggested. Many thanks! :-) $\endgroup$
    – vindobona
    Nov 20 at 10:30
7
$\begingroup$
tripel = {{5, 10, 9}, {4, 9, 3}, {3, 9, 8}, {4, 5, 9}, {9, 10, 8}};

tripel /. {{a_, 9, c_} :> {9, c, a}, {a_, b_, 9} :> {9, a, b}}

{{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {9, 10, 8}}

$\endgroup$
1
  • 1
    $\begingroup$ Thanks, that looks better readable than my Which- version $\endgroup$ Nov 20 at 9:21
7
$\begingroup$
RotateLeft[#, PositionIndex[#] @ 9 - 1] & /@ tripel
{{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {9, 10, 8}}

To generalize to the case where 9 does not appear in some triples, replace PositionIndex[#] @ 9 with

Replace[_Missing -> 1] @ PositionIndex[#] @ 9
$\endgroup$
5
$\begingroup$

Perhaps:

NestWhile[RotateLeft, #, #[[1]] != 9 &] & /@ tripel

yields: {{9, 5, 10}, {9, 3, 4}, {9, 8, 3}, {9, 4, 5}, {9, 10, 8}}

$\endgroup$
5
$\begingroup$
 MapThread[RotateLeft,{#,Position[#,9][[All,2]]-1}]&[tripel]

(* {{9,5,10},{9,3,4},{9,8,3},{9,4,5},{9,10,8}} *)
$\endgroup$

Your Answer

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

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