32 votes
Accepted

Optimization of function taking a permutation

How about a Monte-Carlo-Metropolis search? I'll implement a simplistic version here. See complete universal code further down. Update: Cleaned-up code now available in the Wolfram Function Repository, ...
user avatar
  • 36.9k
28 votes
Accepted

Faster derangements?

Chunks of derangements Since I've already written library link code generating permutations, generating derangements requires just few tweaks: ...
user avatar
  • 14.8k
25 votes

Permutations[Range[12]] produces an error instead of a list

Since Mathematica 8 it is possible generate the elements of any group one by one with GroupElements. Here's for example a randomly chosen element of the permutation ...
user avatar
  • 5,921
25 votes

Faster derangements?

This is the fastest method I have come up with: ...
user avatar
  • 265k
21 votes

Fastest way to generate {{a,a},{a,b},{a,c},{b,b},{b,c},{c,c}} from {a,b,c}

Select[Tuples[{a,b,c},2],OrderedQ]
user avatar
  • 6,734
18 votes

Faster derangements?

Here is one way to generate them directly: it is based on a way to generate all permutations but discards invalid ones early: ...
user avatar
  • 8,574
18 votes
Accepted

Delete duplicates from list of lists as if on a necklace

A high-performance solution Since you are planning to work with thousands of necklaces, it may be much faster to introduce a canonical form. The main point is to write a necklace canonization ...
user avatar
  • 7,676
17 votes

Permutations of nested parentheses (Dyck words)

StringReplaceList I just realized that there is a comparatively clean though not highly efficient way to write this using ...
user avatar
  • 265k
17 votes

Terse Method to Swap Lowest for Highest?

How about: Module[{tmp = test}, With[{ord=Ordering[tmp]}, tmp[[ord]] = Reverse @ tmp[[ord]]]; tmp ] {56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, ...
user avatar
  • 124k
17 votes
Accepted

StringContainsQ, but anywhere in order

StringMatchQ["aabbc", "*a*c*"] True StringMatchQ["aabbc", "*b*a*"] False You can also use ...
user avatar
  • 350k
16 votes
Accepted

Generating Tuples with restrictions

Select[ IntegerPartitions[24, {8}, Range[5]], #.# == 86 & ] ...
user avatar
  • 133k
16 votes
Accepted

ls Ordering[Ordering[list]] optimal?

No, Ordering[Ordering[list]] not optimal. And yes, there is a faster method: ...
user avatar
15 votes
Accepted

Check if two lists are equal in any order

Is it any permutation ? Then I think then this should do areListsEqual[List1_,List2_]:=(Sort[List1]===Sort[List2]);
user avatar
  • 3,254
15 votes
Accepted

List all possible license plate numbers

I would use a = Alphabet[]; (* letter *) d = Range[0, 9]; (* digit *) result = Tuples[{a, a, d, d, d, d}];
user avatar
  • 227k
14 votes

Permutations of lists of fixed even numbers

The permutation you described is called "derangement". There is a function Derangement in Combinatoricapackage. ...
user avatar
  • 7,681
14 votes
Accepted

Permutations of lists of fixed even numbers

Permutations where no element remains in its original place are called derangements. Counting them is easy enough: the number of derangements of a set of size $n$ is $!n$, or the subfactorial of $n$. ...
user avatar
  • 8,574
14 votes

StringContainsQ, but anywhere in order

Is this what you need? StringContainsQ["aabbc","a" ~~ ___ ~~ "c"] True The following documentation pages should help you get going with string patterns in ...
user avatar
  • 18.8k
14 votes
Accepted

Fastest way to generate {{a,a},{a,b},{a,c},{b,b},{b,c},{c,c}} from {a,b,c}

GroupTheory`Tools`Multisets[{a, b, c}, 2] {{a,a},{a,b},{a,c},{b,b},{b,c},{c,c}}
user avatar
  • 24.5k
13 votes
Accepted

Number by permutation

Just a note - any method generating the permutations and the searching will get very slow very quickly, and blow RAM soon after. Something like this s/b much more efficient (e.g., on my goof-top, for ...
user avatar
  • 25.1k
13 votes

Delete duplicates from list of lists as if on a necklace

Preface If one could create a function f that calculates a canonical form of a necklace that turns all equivalent necklaces ...
user avatar
  • 111k
13 votes
Accepted

Generating a list of integers that sums to zero

...
user avatar
  • 36.9k
12 votes

Check if two lists are equal in any order

If you have many duplicates (and the number of duplicates matter) then Tally can improve performance sufficiently ...
user avatar
  • 42.9k
12 votes
Accepted

What is the fastest way to get the nth distinct permutation of a list?

A simple recursive function I've used many times: ...
user avatar
  • 7,644
12 votes
Accepted

Cyclic and Non-cyclic Permutations

Per the request, I post my comment as an answer: First question cy := Permute[#, CyclicGroup[Length@#]] & cy[Range@5] {{1, 2, 3, 4, 5}, {2, 3, 4, 5, 1}, {3, 4,...
user avatar
  • 24.5k
12 votes
Accepted

Can we turn this for loop into a more elegant Mathematica code?

Apparently, you try to apply a permutation given by list indirection to a vector duplicate. Here are several ways to do it, ...
user avatar
12 votes

Fastest way to generate {{a,a},{a,b},{a,c},{b,b},{b,c},{c,c}} from {a,b,c}

Another possibility is to use Pick: ...
user avatar
  • 124k
11 votes

Permutations on elements a>b && b>c

Subsets[Reverse@Range[5], {3}] {{5, 4, 3}, {5, 4, 2}, {5, 4, 1}, {5, 3, 2}, {5, 3, 1}, {5, 2, 1}, {4, 3, 2}, {4, 3, 1}, {4, 2, 1}, {3, 2, 1}} ...
user avatar
  • 350k
11 votes
Accepted

Connectivity in a molecule and permutations

Getting the graph from the XYZ file You have only a set of coordinates and atom types in an XYZ file. When you import it in Mathematica you can import 3 elements: the 3D plot, the coordinates, and ...
user avatar
  • 63.5k
11 votes

Faster derangements?

Here is a straightforward compiled implementation of Knuth's "Algorithm X" for lexicographically generating restricted permutations, specialized to the derangement case: ...
user avatar
11 votes

Delete duplicates from list of lists as if on a necklace

...
user avatar
  • 350k

Only top scored, non community-wiki answers of a minimum length are eligible