2
$\begingroup$

This question may have a simple answer, but I can't find it anywhere in the documentation. I'm trying to find a function that tells me whether a list is a permutation of another list. So the function should behave like this:

TestPermutation[{1, 2, 3, 4}, {1, 3, 2, 4}] (* Should return True *)

TestPermutation[{1, 2, 3, 4}, {1, 1, 2, 4}] (* Should return False *)

What sort of works is to use FindPermutation[list1, list2], and then catch the error that says e.g.:

FindPermutation::norel: Expressions {1,2,3} and {2,1,2} cannot be related by a permutation. >>

But that isn't a very nice solution. Is there a neat way do do this?

$\endgroup$
  • 13
    $\begingroup$ Sort[list1]==Sort[list2] $\endgroup$ – george2079 Jun 13 '14 at 11:19
  • 2
    $\begingroup$ @george2079 - pity, that you didn't post your comment as an answer - too trivial? $\endgroup$ – eldo Jun 13 '14 at 16:31
  • $\begingroup$ i think you can test if the Complement of the first list with the second one is 0 $\endgroup$ – Alucard Sep 4 '16 at 3:50
5
$\begingroup$

so this doesn't sit unanswered..

 testPermutation[ list1_List , list2_List ] := Sort[list1] == Sort[list2] ; 
 testPermutation[{1, 2, 3, 4}, {1, 3, 2, 4}]
 testPermutation[{1, 2, 3, 4}, {1, 1, 2, 4}]

True

False

or generalized to multiple lists:

 testPermutation[ list__List  ] := Equal @@ Sort /@ List@list
 testPermutation[{1, 2, 3, 4}, {1, 3, 2, 4}, {1, 2, 4, 3}]
 testPermutation @@ Permutations[{1, 2, 3, 4}]

True

True

$\endgroup$
  • $\begingroup$ SameQ or MatchQ may be preferable to Equal. $\endgroup$ – Oleksandr R. Jun 14 '14 at 12:42
2
$\begingroup$

Not so nice as george2079's answer but maybe faster with large matrices (avoids sorting):

testPermutation[m_] := 
 Length@Flatten@
      Table[Position[m[[i]], m[[1, j]]], {i, 2, Length@m}, {j, 1, 
        Length@First@m}] == (Times @@ # - Last@#&@Dimensions@m)

testPermutation@{{1, 2, 3, 4}, {1, 3, 2, 4}, {1, 2, 4, 3}}

True

testPermutation@{{1, 2, 3, 4}, {1, 3, 2, 4}, {5, 2, 4, 3}}

False

$\endgroup$
1
$\begingroup$

Another simple solution could be based on Cycles[]. The existence of duplicates is also tested.

testPermutation[l1_List, l2_List] :=
 With[{
   len1 = Length@l1,
   lu1 = CountDistinct@l1,
   lu2 = CountDistinct@l2}, 
  And[lu1 == lu2 == len1, Cycles[{l1}] == Cycles[{l2}]]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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