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

Re: Add-A-Gram Performance

by dfog (Scribe)
on Feb 06, 2002 at 21:05 UTC ( [id://143698]=note: print w/replies, xml ) Need Help??


in reply to Add-A-Gram Performance

My entry into the pot works by starting with the smallest words and building to the largest, instead of the other way. On the word list from ITA, the answers are 16 letters long (Underestimations and indeterminations).
#!perl use strict; use Data::Dumper; $|++; my %WordTree = (); my %WordLengths = (); my $LongWordLength = 1; open (DATA, "Word.txt"); while (<DATA>) { chomp; my $Word = $_; my $Length = length($_); next if $Length < 3; $LongWordLength = $Length if ($LongWordLength < $Length); push (@{$WordLengths{$Length}}, $Word); } print "Finished reading Data\n"; close (DATA); for (my $CurrentLength = 3; $CurrentLength <= $LongWordLength; $Curren +tLength++) { print "Working on words of length $CurrentLength\n"; my ($Word) = ""; foreach $Word (@{$WordLengths{$CurrentLength}}) { my (@SortedLetters) = sort split //, $Word; if ($CurrentLength > 3) { my $TempWord = ""; my @SplicedLetters = ""; my $MatchFlag = 0; for (my $Splice = 0; $Splice <= $#SortedLetters; $Splice++ +) { @SplicedLetters = @SortedLetters; splice(@SplicedLetters, $Splice, 1); $TempWord = join '', @SplicedLetters; if (exists($WordTree{$#SortedLetters}->{$TempWord})) { if (! exists($WordTree{$CurrentLength}->{join '', +@SortedLetters})) { push (@{$WordTree{$CurrentLength}->{join '', @ +SortedLetters}}, $Word); push (@{$WordTree{$CurrentLength}->{join '', @ +SortedLetters}}, @{$WordTree{$CurrentLength - 1}->{join ' +', @SplicedLetters}}); } last; } } } else { if (! exists($WordTree{3}->{join '', @SortedLetters})) { push (@{$WordTree{3}->{join '', @SortedLetters}}, $Wor +d); } } } } my (@LongestMatch) = sort {$a<=>$b} keys %WordTree; my $NotFoundLongest = 1; while ($NotFoundLongest && $#LongestMatch > 1) { if (keys %{$WordTree{$LongestMatch[$#LongestMatch]}} > 0) { print "The longest length is $LongestMatch[$#LongestMatch]\n"; print Dumper $WordTree{pop @LongestMatch}; $NotFoundLongest = 0; } pop @LongestMatch; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-23 17:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found