[issue12961] itertools: unlabelled balls in boxes

Mark Dickinson report at bugs.python.org
Mon Sep 19 15:41:04 CEST 2011


Mark Dickinson <dickinsm at gmail.com> added the comment:

And using combinations_with_replacement, it's even a one-liner:

>>> balls_in_boxes = lambda n, k: (tuple(map(c.count, range(k))) for c in combinations_with_replacement(range(k), n))
>>> for item in balls_in_boxes(5,3): print(item)
... 
(5, 0, 0)
(4, 1, 0)
(4, 0, 1)
(3, 2, 0)
(3, 1, 1)
(3, 0, 2)
(2, 3, 0)
(2, 2, 1)
(2, 1, 2)
(2, 0, 3)
(1, 4, 0)
(1, 3, 1)
(1, 2, 2)
(1, 1, 3)
(1, 0, 4)
(0, 5, 0)
(0, 4, 1)
(0, 3, 2)
(0, 2, 3)
(0, 1, 4)
(0, 0, 5)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12961>
_______________________________________


More information about the Python-bugs-list mailing list