Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: scrambling all letters in a word until all combinations are found

by cmeyer (Pilgrim)
on Feb 03, 2006 at 18:07 UTC ( [id://527755]=note: print w/replies, xml ) Need Help??


in reply to Re^2: scrambling all letters in a word until all combinations are found
in thread scrambling all letters in a word until all combinations are found

# takes an integer word length and a list of letters, # returns a list of words of the integer length # { my %cache; sub wordz { my ( $n, @letters ) = @_; my $cache_key = join( '', @letters, $n ); return if $n > @letters; return @letters if $n == 1; return @{ $cache{ $cache_key } } if exists $cache{ $cache_key }; my @wordz; for ( my $i = 0; $i <= $#letters; $i++ ) { push @wordz, map { $letters[ $i ] . $_ } wordz( $n - 1, @letters[ 0 .. $i - 1 ], @letters[ $i + 1 .. $#letters ] ); my @o = wordz( $n, @letters[ $i + 1 .. $#letters ] ); push @wordz, @o; } $cache{ $cache_key } = \@wordz; return @wordz; } }
It is also fun to do this problem non-recursively. If you are interested in that, I also have code (though there are different possible approaches).

-Colin.

WHITEPAGES.COM | INC

  • Comment on Re^3: scrambling all letters in a word until all combinations are found
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://527755]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-23 13:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found