Place n indistinguishable items into k distinguishable boxes

Michael Robertson mcrobertson at hotmail.com
Wed Feb 27 21:47:25 EST 2008


Michael Robertson wrote the following on 02/27/2008 06:40 PM:
> Hi,
> 
> I need a generator which produces all ways to place n indistinguishable 
> items into k distinguishable boxes.
> 

My first thought was to generate all integer partitions of n, and then 
generate all permutations on k elements.  So:

4 = 4
   = 3 + 1
   = 2 + 2
   = 2 + 1 + 1

Then for 4,  generate all permutations of x=(4,0,0,..),  |x|=k
Then for 3,1 generate all permutations of x=(3,1,0,..),  |x|=k
Then for 2,2 generate all permutations of x=(2,2,0...),  |x|=k
...

In addition to having to generate permutations for each integer 
partition, I'd have to ignore all integer partitions which had more than 
k parts...this seemed like a bad way to go (bad as in horribly inefficient).

Better ideas are appreciated.



More information about the Python-list mailing list