in reply to scrambling all letters in a word until all combinations are found
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: scrambling all letters in a word until all combinations are found
by Anonymous Monk on Jan 23, 2006 at 20:18 UTC | |
by cmeyer (Pilgrim) on Feb 03, 2006 at 18:07 UTC |