Place n indistinguishable items into k distinguishable boxes

castironpi at gmail.com castironpi at gmail.com
Thu Feb 28 05:46:29 CET 2008


On Feb 27, 10:41 pm, Mark Dickinson <dicki... at gmail.com> wrote:
> On Feb 27, 11:38 pm, Mark Dickinson <dicki... at gmail.com> wrote:
>
> >             yield map(len, (''.join(s)).split('|'))
>
> That line should have been just:
>
>             yield map(len, s.split('|'))
>
> of course.
>
> Mark

It's easier:

def rec( boxesleft, stonesleft, seq ):
	if 1== boxesleft:
		print( seq+ ( stonesleft, ) )
		return
	for i in range( stonesleft+ 1 ):
		rec( boxesleft- 1, stonesleft- i, seq+ ( i, ) )
rec( 3, 4, () )
rec( 6, 1, () )
rec( 4, 2, () )

Just sort the list in text-ascending order, and it's pretty clear.

It uses tuple concat., which may be slower than Marks.

If you want an iterative, stay tuned.



More information about the Python-list mailing list