This snippet allows you to find the number of ordered permutations of items taken from a population of known size. It uses the well know algorithm with factorials. For very large populations you may want to use an approximation of factorials.
Number of Permutations (Python)
''' permutation_number1.py
find the number of ordered permutations of items
taken from a population of known size
see also:
http://www.mathwords.com/p/permutation_formula.htm
tested with Python27/33 and IronPython273 by vegaseat 27feb2013
'''
from math import factorial
def number_permutations(n, k):
'''
return the number of ordered permutations of
k items taken from a population of size n
uses algorithm n!/(n-k)!
'''
return factorial(n)/factorial(n-k)
q = '''\
How many ways can 4 students from a group of 15
be lined up for a photograph?
'''
print(q)
print(number_permutations(15, 4))
''' result ...
How many ways can 4 students from a group of 15
be lined up for a photograph?
32760
'''
q2 = '''
How many ways can you permutate 'abc'?
'''
print(q2)
print(number_permutations(3, 3))
''' result ...
How many ways can you permutate 'abc'?
6
'''
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.