Re: please help find logic error in home-brewed brute force anagram solver/creator script
- From: peterc@xxxxxxxxxxxxxx (Peter Cornelius)
- Date: Fri, 30 Dec 2005 15:40:23 -0800
Jupiter host has a good point about not reinventing the wheel but it might still be educational to fix this code. There's an interesting warning if you run this with '-w':
word=NERO |NERO| |ENRO| |RNEO| |ONER| Use of uninitialized value in join or string at ./anagram.pl line 33. |NERO| Use of uninitialized value in join or string at ./anagram.pl line 33.
If you take a look at your loop conditions you should be able to figure out why this is happening. You might use the other version of the 'for' construct to keep you from hitting this again.
for my $word (@words) {
....
}-PC
On Dec 30, 2005, at 12:23 PM, Wolcott, Kenneth A wrote:
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?
Secondly, on the matter of efficiency, I could push the results into a
hash, then sort the hash to print out unique permutations. Even better
yet, a better algorithm that wouldn't generate multiple permutations.
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
################# use strict;
my @words = ("NERO", "TOOLED", "MEALS", "DOMAINS", "NERVED", "TRADUCE",
"COUNTS", "SALVAGES", "DIAGNOSE", "AGROUND");
for (my $w=0; $w<@words; $w++) { print "word=".$words[$w]."\n"; my @letters = split //, $words[$w]; for (my $i=0; $i<=length($words[$w]); $i++) { for (my $j=0; $j<=length($words[$w]); $j++) { # next if ($i == $j); ($letters[$i], $letters[$j]) = ($letters[$j], $letters[$i]); my $new_word = join "", @letters; print "|".$new_word . "|\n"; } } } #################
.
- References:
- please help find logic error in home-brewed brute force anagram solver/creator script
- From: Kenneth A Wolcott
- please help find logic error in home-brewed brute force anagram solver/creator script
- Prev by Date: Re: Use of uninitialized value Error
- Next by Date: Re: Date formatting
- Previous by thread: Re: please help find logic error in home-brewed brute force anagram solver/creator script
- Next by thread: Re: please help find logic error in home-brewed brute force anagram solver/creator script
- Index(es):