3 digit permutations with no duplicate combination of 0-9 in Python

6.6k Views Asked by At

I'm trying output all of the 3 digit permutation combinations of 0-9 and something in my code is wrong because instead of getting 1000 possible permutation combinations I'm only getting 720. Is it that a permutation won't allow for something like "000" or "111" or "333", etc all the way through to "999"? If those are being excluded where are the other 270 permutation combinations?

I pretty new to Python so be gentle. :)

Here is my code:

# three digit permutations of 0-9

import itertools
import random

data = list(itertools.permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 3))
# Output a random permutation
one = random.choice(data)
print("Here is a random three digit permutation from zero to nine: " + str(one)
      + "\n")

# Output a random permutation
one = random.choice(data)
print("Here is another random three digit permutation from zero to nine: " +
      str(one) + "\n")

# Output a random permutation
one = random.choice(data)
print("And yet another random three digit permutation from zero to nine: " +
      str(one) + "\n")

# Output total number of permutation
num_perm = len(data)
print("The number of permutation combinations is: " + str(num_perm))

Test output:

Here is a random three digit permutation from zero to nine: (2, 9, 5)

Here is another random three digit permutation from zero to nine: (4, 5, 7)

And yet another random three digit permutation from zero to nine: (9, 4, 6)

The number of permutation combinations is: 720

1

There are 1 best solutions below

0
On BEST ANSWER

Just like you have in the comments, when Python is doing permutations, you should, and do get $720 = 10 \cdot 9 \cdot 8 $. This means that neither $211$ or $112$ will not be in your list of all permutations:

data = list(itertools.permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 3))

This is because when you do itertools permutations:

  • you have $10$ options to pick from for the first digit.
  • you have $9$ options to pick from for the second digit (all digits but the one from the first digit)
  • you have $8$ options to pick from for the third digit (all digits but the one from the first and second digits)