Mathematica Asked by Money Oriented Programmer on February 21, 2021
I want to simplify the following pattern
Cases[Permutations[Range@3], {___, 2, 3, ___} | {___, 3, 2, ___}]
into something like (but it does not compile).
Cases[Permutations[Range@3], {___, (2, 3) | (3, 2), ___}]
Please let me know if it can be simplified.
This updated example has an easy fix with OrderlessPatternSequence
.
Permutations[Range@3]
Cases[%, {___, OrderlessPatternSequence[2, 3], ___}]
{{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}
{{1, 2, 3}, {1, 3, 2}, {2, 3, 1}, {3, 2, 1}}
Side note: That being said. I was working on this before the edit and I noticed interesting, seemingly wrong, behavior.
Basically, naming a pattern effects matching. For example
MatchQ[{1, 4, 3, 2}, {l___, seq : OrderlessPatternSequence[2, 3], r___}]
returns False
where as
MatchQ[{1, 4, 3, 2}, {l___, OrderlessPatternSequence[2, 3], r___}]
returns True
. It's also interesting and should be noted that
MatchQ[{1, 4, 2, 3}, seq : {l___, OrderlessPatternSequence[3, 2], r___}];
MatchQ[{1, 4, 2, 3}, {l___, OrderlessPatternSequence[3, 2], r___}];
%% === % === True
True
There's also this confirmed bug to a similar situation. This might be better actually asked a separate question, but I just wanted to see if there was something obvious I'm missing.
Correct answer by NonDairyNeutrino on February 21, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP