Quoting Matthew Wickline (listbox@wickline.org): > What I'd do: > Assume that $guess *is* the target word. Then, you can > derive how many jotts $probe *should* have received. Return > true if and only if the number of jotts derived is the same > as the actual number of $jotts for $probe. > > So now the problem is reduced to finding the number of jotts > a probe should get for a given target word. I don't know > that this is the fastest way to do it, but this seems good > to me: > > sub get_jotts { > my($probe, $target) = @_; > my %letters; > for ( split('',$target) ) > { $letters{$_}++ }; > my $jotts = 0; > for ( split('',$probe) ) > {$jotts++ if $letters{$_}-- > 0}; > $jotts; > } You can use the fact that you're calling your routine repeatedly with one word remaining the same to optimise it. This is what I did: $code=eval "sub { my(\$t)=shift; \$t=~s/". join("//+\$t=~s/",split//,$guessed_word)."//;}"; @viable_words=grep(&$code($_)==$score,@viable_words); So, if the guessed word is "taste" the code expands to sub { my($t)=shift; $t=~s/t//+$t=~s/a//+$t=~s/s//+$t=~s/t//+$t=~s/e//;} to see how this works, run it through for a word like "least": s/t// + s/a// + s/s// + s/t// + s/e// least leas les le le l 1 + 1 + 1 + 0 + 1 = 4 in the case where the guessed word had a score of 5, I did a second grep to remove the guessed word from the list (it gets removed anyway for lower scores). I think an interesting "jotto" problem is to find the words that require the most number of guesses for this algorithm. My code to do this in Perl took an unacceptable amount of time to run, so I recoded it in C, which took me an unacceptable amount of time to write, but I don't think it's working right, as it keeps claiming "axers" is a good choice, but I'm sure "taste" is better. -- Adam Rice -- wysiwyg@glympton.airtime.co.uk -- Blackburn, Lancashire, England ==== Want to unsubscribe from Fun With Perl? Well, if you insist... ==== Send email to <fwp-request@technofile.org> with message _body_ ==== unsubscribe