Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50049
| References | <5d22d723-bf1b-467d-b9d3-a9a814230309@googlegroups.com> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2013-07-06 01:11 -0600 |
| Subject | Re: calculating binomial coefficients using itertools |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4328.1373094710.3114.python-list@python.org> (permalink) |
On Fri, Jul 5, 2013 at 4:58 PM, Robert Hunter <bobjim.hunter@gmail.com> wrote:
> 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:]
Nice, I like seeing interesting ways to use slice. This will be more
efficient, though:
def binomial(n):
value = 1
for i in range(n+1):
yield value
value = value * (n-i) // (i+1)
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
calculating binomial coefficients using itertools Robert Hunter <bobjim.hunter@gmail.com> - 2013-07-05 15:58 -0700 Re: calculating binomial coefficients using itertools Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-06 01:11 -0600
csiph-web