Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.031 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.01; 'indices': 0.07; 'though:': 0.07; 'slices': 0.09; 'subject:using': 0.09; 'def': 0.12; 'count,': 0.16; 'itertools': 0.16; 'slice.': 0.16; 'starmap': 0.16; 'wrote:': 0.18; 'import': 0.22; 'header:In-Reply-To:1': 0.27; 'robert': 0.30; 'message-id:@mail.gmail.com': 0.30; 'fri,': 0.33; 'received:google.com': 0.35; 'yield': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'more': 0.64; 'jul': 0.74; 'hunter': 0.84; 'nice,': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=yfmqlb7XKIXPG1snRAcKsSxG2js/+Op7xaQ3McWClpI=; b=utWrDRHT4dUggPHBn+x9zls49TyX9Rz3Z328cEdbwyUlzMu1EqCTxq21OnWg6BWdS5 BbYPQqtgTtMGd4PT7Tpy48y8fpAGv1RLik+j7MnNBQ+viOGpXq9p7ejUoo18SPneFNeo 0SlkYPhNkrr8yW6EGCP+4P7Pc/hs30qZn/8tjh/eeMyfFlgbIDxFfj3hpZbr1a/v/lDo w63asEW+3k6qKgyMXVguS8e8h3Ka5UAioamThKtCueMxT5K6IbK/s31mOIEq5YgRmyN4 ohEMW3ZMuzSK758WCIGVUi9O1QRzV47EOM6yShxv1GaiygMtrq6ld2uAzSwVgFDBTy+W g/+A== X-Received: by 10.66.249.202 with SMTP id yw10mr14129374pac.145.1373094701702; Sat, 06 Jul 2013 00:11:41 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <5d22d723-bf1b-467d-b9d3-a9a814230309@googlegroups.com> References: <5d22d723-bf1b-467d-b9d3-a9a814230309@googlegroups.com> From: Ian Kelly Date: Sat, 6 Jul 2013 01:11:01 -0600 Subject: Re: calculating binomial coefficients using itertools To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373094710 news.xs4all.nl 15942 [2001:888:2000:d::a6]:46815 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50049 On Fri, Jul 5, 2013 at 4:58 PM, Robert Hunter 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)