Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '(also': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'def': 0.15; '(2,': 0.16; 'iterator': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'written': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'appears': 0.20; 'this?': 0.21; 'raymond': 0.21; 'discussion': 0.22; 'work,': 0.23; 'extensively': 0.23; "python's": 0.24; 'code': 0.25; 'suggestion': 0.26; 'skip:b 20': 0.26; '(the': 0.28; 'problem': 0.28; 'import': 0.28; 'print': 0.29; 'module': 0.30; 'from:addr:web.de': 0.30; 'class': 0.30; 'does': 0.32; 'anyone': 0.32; "isn't": 0.33; 'to:addr:python- list': 0.33; "i've": 0.34; 'creates': 0.34; 'convince': 0.34; 'header:X-Complaints-To:1': 0.35; 'rather': 0.35; 'post': 0.36; 'skip:" 10': 0.36; 'but': 0.37; 'received:org': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'suggestions': 0.39; 'header:Mime- Version:1': 0.39; 'appreciated.': 0.39; 'to:addr:python.org': 0.39; "i'd": 0.40; 'your': 0.61; 'contact': 0.66; 'dr.': 0.68; 'misses': 0.84; 'numbered': 0.84; 'subject:balls': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: recursive algorithm for balls in numbered boxes Date: Sun, 11 Sep 2011 14:48:41 +0200 Organization: None References: <32440187.post@talk.nabble.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084aa93.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1315745317 news.xs4all.nl 2436 [2001:888:2000:d::a6]:41930 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:13125 Dr. Phillip M. Feldman wrote: > I've written a recursive class that creates an iterator to solve a general > formulation of the combinatorics problem known as "balls in numbered > boxes" > (also known as "indistinguishable balls in distinguishable boxes"). The > code has been extensively tested and appears to work, but isn't terribly > elegant. Any suggestions about how to improve it will be appreciated. Does the following do what you want? >>> from itertools import product >>> def binb(balls, boxsizes): ... return (fill for fill in product(*[range(bs+1) for bs in boxsizes]) if sum(fill) == balls) ... >>> for item in binb(10, [4, 3, 2, 1, 2]): ... print item ... (2, 3, 2, 1, 2) (3, 2, 2, 1, 2) (3, 3, 1, 1, 2) (3, 3, 2, 0, 2) (3, 3, 2, 1, 1) (4, 1, 2, 1, 2) (4, 2, 1, 1, 2) (4, 2, 2, 0, 2) (4, 2, 2, 1, 1) (4, 3, 0, 1, 2) (4, 3, 1, 0, 2) (4, 3, 1, 1, 1) (4, 3, 2, 0, 1) (4, 3, 2, 1, 0) If so, your implementation misses a few configurations: >>> from balls_in_numbered_boxes import balls_in_numbered_boxes as bb >>> for item in bb(10, [4, 3, 2, 1, 2]): ... print item ... [4 3 2 1 0] [3 3 2 1 1] [2 3 2 1 2] > Also, I'd like to get this functionality into the Python's `itertools` > module (the present set of combinatorics functions in `itertools` does not > include "balls in boxes"). Does anyone know whom I should contact about > this? Basically you have to convince Raymond Hettinger. I recommend that you post your suggestion on python-ideas for a general discussion rather than approaching him directly.