Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Word Unscrambler

by Util (Priest)
on Dec 17, 2005 at 03:44 UTC ( #517430=note: print w/replies, xml ) Need Help??


in reply to Word Unscrambler

If speed is your concern, I would recommend using a pre-built regexp for fast filtering of the candidate words, and a slower exact test against a canonical (sorted letters) form.

Tested, working code:

use strict; use warnings; my $dict = '/usr/share/dict/words'; # Do not space-separate the word in this version. my $scrambled_word = shift or die "Must specify a word\n"; my $scrambled_length = length $scrambled_word; my $scrambled_sorted = join '', sort split '', $scrambled_word; my $pattern = qr{ \A (?: [$scrambled_word]{$scrambled_length} ) \n \z }x; open DICT, '<', $dict or die "Cannot open '$dict': $!"; while (<DICT>) { next unless /$pattern/o; chomp; my $sorted = join '', sort split '', $_; next unless $sorted eq $scrambled_sorted; print "$_\n"; } close DICT or warn;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://517430]
help
Chatterbox?
[LanX]: machiato!
shmem fills a glas with sambuca, drops 3 beans of coffee into it, lights the stuff and hands it to erix
[Your Mother]: Liquore Strega. :P
[shmem]: s/beans of coffee/enterprise beans/ :-P
[erix]: Take your bottles -- you're all invited! :)

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2017-03-28 12:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (330 votes). Check out past polls.