good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: Combinatoricsby bart (Canon) |
on Aug 22, 2002 at 09:12 UTC ( [id://191960]=note: print w/replies, xml ) | Need Help?? |
Ah, a twist. The common question is about permutations. Nevertheless, I'd still use recursion. Say you need $n elements. Is the first element included? If yes, combine it with all combinations of $n-1 elements from the list of all of the following elements. If no, get all combinations of $n elements of the same sublist. Of course, you need to include every possible solution, which means walking every possible path. Code!
It appears to be working well. Update: I'm pretty sure inserting at the appropriate place, i.e. among the other return statements near the top, will improve efficiency quite a bit. It avoids doing a lot of useless recursion if all you want is the empty list as a singleton.
In Section
Seekers of Perl Wisdom
|
|