Re: please help find logic error in home-brewed brute force anagram solver/creator script




----- Original Message ----- From: ""Wolcott, Kenneth A"" <KWOLCOTT@xxxxxxxxx>
Newsgroups: perl.beginners
To: <beginners@xxxxxxxx>
Sent: Friday, December 30, 2005 3:23 PM
Subject: please help find logic error in home-brewed brute force anagram solver/creator script



Hi;

 I have enclosed both the perl source and the associated output of a
bruce-force anagram script that I wrote that has a logic error in it
that I can't find.

 It does not generate all of the permutations of the letters in the
words specified.  Why not?

[snip]

And one better yet, usage of an external dictionary lookup to eliminate
the permutations that aren't words.  But I need help on the logic error
first :-)

Thanks,
Ken Wolcott

Hello Ken,

The code (and its results below) indeed do check each word against a dictionary.

Chris


#!/usr/bin/perl use strict; use warnings;

my %dict;
while (<>) {
   chomp;
   my $key = lc join "", sort split "";
   push @{$dict{$key}}, $_;
}

my @words = ("NERO", "TOOLED", "MEALS", "DOMAINS", "NERVED", "TRADUCE",
"COUNTS", "SALVAGES", "DIAGNOSE", "AGROUND");

for (@words) {
   my $key = lc join "", sort split "";
   if ($dict{$key}) {
      print "$_: @{$dict{$key}}\n";
   }
   else {
      print "$_\n";
   }
}

__END__

C:\perlp>perl t4.pl \usr\dict\wordnet
NERO: nero reno
TOOLED: looted toledo
MEALS: males meals salem selma
DOMAINS: domains madison
NERVED: denver vender
TRADUCE: traduce
COUNTS: counts tucson
SALVAGES: salvages
DIAGNOSE: diagnose
AGROUND: aground


.