combination function in python

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sun Apr 15 05:38:31 EDT 2007


DanielJohnson:
> Please help, I couldnt find the function through help.

You can't find it because it's not there:

def factorial(n):
    """factorial(n): return the factorial of the integer n.
    factorial(0) = 1
    factorial(n) with n<0 is -factorial(abs(n))
    """
    result = 1
    for i in xrange(1, abs(n)+1):
        result *= i
    if n >= 0:
        return result
    else:
        return -result

def binomial(n, k):
    """binomial(n, k): return the binomial coefficient (n k)."""
    assert n>0 and isinstance(n, (int, long)) and isinstance(k, (int,
long))
    if k < 0 or k > n:
        return 0
    if k == 0 or k == n:
        return 1
    return factorial(n) // (factorial(k) * factorial(n-k))

Bye,
bearophile




More information about the Python-list mailing list