Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: (un)jumbler...

by ikegami (Patriarch)
on Nov 09, 2004 at 17:45 UTC ( [id://406408]=note: print w/replies, xml ) Need Help??


in reply to (un)jumbler...

The following gives you a sorted list, so it should be easy to check against a sorted dictionary (by doing something like a merge sort). It's also memory efficient since it doesn't keep a list of all the permutations in memory.

use Algorithm::Loops qw( NextPermute ); my $word = 'tool'; my @list = sort ' ', $word =~ /(.)/g; my $last_word = ''; while (NextPermute(@list)) { my $new_word = join('', @list); $new_word =~ s/ .*//; next if ($new_word eq $last_word); $last_word = $new_word; print($new_word, $/); }

Output:

l lo loo loot lot loto lt lto ltoo o ol olo olot olt olto oo ool oolt oot ootl ot otl otlo oto otol t tl tlo tloo to tol tolo too tool

Replies are listed 'Best First'.
Re^2: (un)jumbler...
by ikegami (Patriarch) on Nov 09, 2004 at 18:09 UTC

    Update: Made to work with /usr/share/dict/words which contains uppercase words.

    Well, I went and wrote the code to check against the dictionary.

    use strict; use warnings; use Algorithm::Loops qw( NextPermute ); sub get_next_dict_word { my ($dict_fh) = @_; for (;;) { local $_ = <$dict_fh>; return $_ unless defined; my $first_letter = substr($_, 0, 1); return $_ if lc($first_letter) eq $first_letter; } } { die("Specify the word as an argument.$/") unless @ARGV; my $word = lc($ARGV[0]); # DICTIONARY MUST BE SORTED, ONE WORD PER LINE. # UPPERCASE WORDS ARE IGNORED. my $dict_fh; open($dict_fh, '<', '/usr/share/dict/words') or die("Can't open dictionary: $!$/"); my $dict_word = <$dict_fh>; chomp($dict_word) if defined $dict_word; my @list = sort ' ', $word =~ /(.)/g; my $last_permute = ''; while (NextPermute(@list)) { my $permute = join('', @list); $permute =~ s/ .*//; next if ($permute eq $last_permute); $last_permute = $permute; while (defined($dict_word) && $dict_word lt $permute) { $dict_word = <$dict_fh>; chomp($dict_word) if defined $dict_word; } if (defined($dict_word) && $permute eq $dict_word) { print("Found $permute$/"); $dict_word = <$dict_fh>; chomp($dict_word) if defined $dict_word; } else { print("Didn't find $permute$/"); } } } dictionary used --------------- apple dud dude dudette due orange output ------ Didn't find d Didn't find dd Didn't find dde Didn't find ddeu Didn't find ddu Didn't find ddue Didn't find de Didn't find ded Didn't find dedu Didn't find deu Didn't find deud Didn't find du Found dud Found dude Found due Didn't find dued Didn't find e Didn't find ed Didn't find edd Didn't find eddu Didn't find edu Didn't find edud Didn't find eu Didn't find eud Didn't find eudd Didn't find u Didn't find ud Didn't find udd Didn't find udde Didn't find ude Didn't find uded Didn't find ue Didn't find ued Didn't find uedd
      Thank you. You have been most helpful!!! I just need to get the module, and depending onhoe successful that goes...I will be returning to thank you One Thousand more times :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-25 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found