calculating binomial coefficients using itertools

Robert Hunter bobjim.hunter at gmail.com
Sat Jul 6 00:58:17 CEST 2013


from itertools import count, repeat, izip, starmap

def binomial(n):
    """Calculate list of Nth-order binomial coefficients using itertools."""

    l = range(2)
    for _ in xrange(n):
        indices = izip(count(-1), count(1), repeat(1, len(l) + 1))
        slices = starmap(slice, indices)
        l = [sum(l[s]) for s in slices]
    return l[1:]



More information about the Python-list mailing list