__author__ = "Maryam Bahrani" """ Permutation statistics Computes the exact ratio of permuations of a fixed length with no singleton or doubleton cycles. Note that a brute force method is used, so the code is too slow for n >> 10. sample usage: $ python perm.py Number of elements to permute: 10 """ import click import numpy from itertools import permutations """ accepts a permutation that has no singleton or doubleton cycle """ def isValid(a): for i in range(len(a)): if a[i] == i or a[a[i]]==i: return False return True """ Returns the number of permutations of length n with no singleton or doubleton cycles """ def countPerms(n): num = 0 for a in permutations(range(n)): if isValid(a): num += 1 return num @click.command() @click.option('--n', prompt = 'Number of elements to permute', default = "10", help = 'The number of elements to permute.') def run(n): numValid = countPerms(int(n)) click.echo("Number of permutations of length %s with no singleton or doubleton cycles: %s." % (n, numValid)) numAll = numpy.math.factorial(int(n)) click.echo("Number of permutations of length %s with no constraints: %s." % (n, numAll)) click.echo("The ratio: %f." % ((numValid+.0)/numAll,)) if __name__ == '__main__': run()