-2

Say I have this list : [1,1,2,2] I want to run through all the permutations of this. If I print the same permutation will be printed 4 times. For [1,1,1,3] the same one will be printed 6 times, For [1,1,1,3,3] 12.

In General : (a1)!(a2)!...(an)! Is there any function that does that in Python? If no, can you give me an algorithm that does in Python?

michael kova
  • 105
  • 2
  • 8

1 Answers1

0

Are you looking for something like the following?

import math

def repeats(nums):
    counts = dict()
    result = 1

    for n in nums:
        if n in counts:
            counts[n] += 1
        else:
            counts[n] = 1

    for n in counts.keys():
        result *= math.factorial(counts[n])

    return result

print repeats([1, 1, 2, 2])     # prints 4
print repeats([1, 1, 1, 3])     # prints 6
print repeats([1, 1, 1, 3, 3])  # prints 12
Phylogenesis
  • 7,775
  • 19
  • 27