Place n indistinguishable items into k distinguishable boxes

Mark Dickinson dickinsm at gmail.com
Thu Feb 28 11:07:31 EST 2008


On Feb 28, 5:02 am, Michael Robertson <mcrobert... at hotmail.com> wrote:
> Thanks again for your efforts here.  This particular problem didn't
> appear in any course I took...certainly similar problems did.

And here's the obligatory not-very-obfuscated one-liner:

from itertools import combinations as c; boxings=lambda n,k:([s[i
+1]+~s[i] for i in range(k)] for s in [[-1]+list(t)+[n-~k] for t in
c(range(n-~k),k-1)])

You'll need to check out and compile the
latest svn sources to make it work, though.
And it doesn't work when k == 0.

Mark



More information about the Python-list mailing list