good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: scrambling all letters in a word until all combinations are foundby cmeyer (Pilgrim) |
on Jan 23, 2006 at 19:31 UTC ( [id://525030]=note: print w/replies, xml ) | Need Help?? |
If you're looking for a canned solution, try Math::Combinatorics (no personal experience). However, this is a pretty fun problem to code. My brain finds it easiest to think of combinations recursively. For a list of letters, if the desired word length (n) is 1, just return the list. Otherwise iterate through each letter, and find all of the words of length that start with that letter (the current letter appended by all words found in the remaining letters, of length n - 1). Then find the list of words of length n that don't include the current letter. That's it. :) Pseudo code follows (let me know if you'd like a real Perl version; I have one kicking around in my sandbox, as well as a nonrecursive one). function wordz ($n, @list ) { if ( n == 1 ) { return @list } for $current_letter ( @list ) { push @words, map { $current_letter . $_ } wordz( $n - 1, @letters_sans_current_letter; push @words, wordz( $n, @letters_sans_current_letter); } return @words; } -Colin. WHITEPAGES.COM | INC
In Section
Seekers of Perl Wisdom
|
|