[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Words made from letters in a given phrase



On Wed, Aug 04, 1999 at 12:38:35PM -0700, Eli Evans wrote:
> FWPers---
> 
> Last week was Logos Spirit Week, with many (annoying) activities and a
> couple of fun puzzles. The most entertaining puzzle for me was: "How many
> words can you make using the letters in 'Robert David Pritchett' " I don't
> care much for corporate pep rallies, but I sure do love using Perl to cheat
> in events! I used a huge free wordlist
> [http://www.thecorsair.com/words1.zip], and banged out a script. The
> dictionary file is ASCII and has one "word" per line, all lowercase.
> 
> Two solutions are below, mine and a co-worker's. I'm sure you can do it
> (shorter|faster|more efficiently)! Or, of course, more FUN (which could also
> mean longer, slower, and less efficiently)?
> 

I use the ENABLE word list, which contains words suitable for puzzles such
as these (like the Scrabble dictionary, but more comprehensive).
173,727 words.


I timed your script, your coworker's script, and a stripped-down version of
my 'words' submission to the Perl Power Tools project.

~> time eli.pl 'robert david pritchett' words_all > eli.out
19.757u 0.145s 0:25.26 78.7% 0+0k 28+0io 0pf+0w
~> time coworker.pl 'robert david pritchett' words_all > coworker.out
62.217u 0.273s 1:16.56 81.6% 0+0k 1+0io 0pf+0w
~> time wordsppt.pl 'robert david pritchett' words_all > wordsppt.out
1.513u 0.109s 0:02.24 71.8% 0+0k 165+0io 150pf+0w

I think my script will be hard to beat for efficiency.


Ronald



#!/usr/local/bin/perl

my $letters = shift;

my %letters;

$letters = lc $letters;              # convert to lowercase
$letters =~ tr/a-z//cd;              # strip non-letter characters

foreach (split(//, $letters)) {      # store letter counts
    $letters{$_}++;
}

my($word);
WORD:
while (defined($word = <>)) {
                                     # for each word in list
    chomp($word);

    next WORD if ($word =~ /[^$letters]/o);
                                     # verify letters used

    my %word;
    foreach (split(//, $word)) {     # verify letter counts
        $word{$_}++;
        next WORD if ($word{$_} > $letters{$_});
    }

    print "$word\n";                 # success - print word

} # WORD: while (defined($word = <DICT>))


__END__

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe