1

I am stuck on the following problem. I want to find an algorithm that finds all possible permutations of a certain numbers of distinct digits and then put them in a large matrix for instance. For instance for the digits 0, 1 and 2 I want to get the matrix:

2 1 2 0 1 0
1 2 0 2 0 1
0 0 1 1 2 2

Here they are in order but this is not required per se. Does anyone know a nice algorithm to achieve this? Thanks in advance.

1
  • 1
    Most languages have libraries that can do permutations for you - perl python ruby java - don't do work that is already done for you.
    – user40980
    Commented Apr 14, 2013 at 1:58

1 Answer 1

1

Building on MichaelT's comment, here's an example solution in Python:

from itertools import permutations
for p in permutations(['0', '1', '2'], 3):
    print p

Output looks like this:

('0', '1', '2')
('0', '2', '1')
('1', '0', '2')
('1', '2', '0')
('2', '0', '1')
('2', '1', '0')

You'll have to arrange the output to match your desired format.

Your Answer

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

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