11
$\begingroup$

I hope to have your help, if I have

listp = Permutations[Range[7]];

With the following functions, I delete the lists that contain {7,2} and {2,7} of the variable listp

filtro[num_List]:=If[Position[num,7]==Position[num,2]+1,False,True];
sfir[num_List]:=If[Position[num,7]==Position[num,2]-1,False,True];

For example in listp is the permutation {1,4,2,7,6,3,5}, which should be deleted from listp, since it contains {2,7}, also permutation {5,1,6,4 , 3,2,7} should be deleted from listp for the same reason. To make all permutations like those mentioned above be removed from listp I do the following:

ek = Select[listp, filtro];
{Length[listp], Length[ek]}

As you can see 720 permutations of listp have been eliminated, I have only used {2,7} as a criterion, now if I use {7,2} let's see how many permutations are eliminated

do = Select[ek, sfir];
{Length[listp], Length[ek], Length[do]}

You will have already noticed that 720 permutations were erased, in total 1440 permutations of listp have been eliminated.

What I did in the previous section was simply delete all the permutations of {2,7} which are

Permutations[{2,7}]

The problem is that for each permutation I had to build a function, besides I want to erase other permutations, which I enlist so that you know what they are.

Permutations[{1,3,5}];
Permutations[{2,4,6,7}];

If I have counted correctly, I would have to do 30 functions to erase all the permutations that I want from listp. I would like to ask you to please help me carry out this listp debugging, since I thought about using the DeleteCases command but I did not know how to assign the permutations that I want to delete within that command, maybe you know an alternative method to achieve my goal. Any help is welcome since I am stuck in this part. Thanks in advance.

$\endgroup$
0

1 Answer 1

11
$\begingroup$

I think OrderlessPatternSequence is the missing piece. For example,

DeleteCases[listp, {___, OrderlessPatternSequence[2, 7], ___}] 

will delete any list inside listp that contains the sequence 2,7 or 7,2.

If there are other sequences to be deleted you could make a composite pattern with

patt = Alternatives[
   OrderlessPatternSequence[2, 7],
   OrderlessPatternSequence[1, 3, 5],
   OrderlessPatternSequence[2, 4, 6, 7]
   ];

and

DeleteCases[listp, {___, patt, ___}]

will delete any list containing these orderless patterns.

If you are using a version prior to 10.1 then OrderlessPatternSequence is not defined, and you can implement it yourself as

OrderlessPatternSequence[args___] := 
 Alternatives @@ PatternSequence @@@ Permutations@{args}
$\endgroup$
2
  • $\begingroup$ First of all an apology for thanking you so far, the solution that you shared with me works wonderfully, I have tried it online and I get what I was looking for, thanks for your help. I do not know if you have any solution that works in MMA version 10.0, since in that version there is no command OrderlessPatternSequence, that would be extra because I have that version on desktop, thank you again $\endgroup$ Jul 4, 2018 at 0:53
  • $\begingroup$ No need to apologize at all. See the edit for an alternative definition. $\endgroup$
    – Jason B.
    Jul 4, 2018 at 1:58

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.