Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: challanging the dictionary

by Limbic~Region (Chancellor)
on Apr 23, 2006 at 23:11 UTC ( #545167=note: print w/replies, xml ) Need Help??


in reply to Re: challanging the dictionary
in thread challenging the dictionary

blokhead,
This seems to be a relatively fast approximation algorithm (64K words in about 5 seconds):
#!/usr/bin/perl use strict; use warnings; my $file = $ARGV[0] || 'words.txt'; open(my $fh, '<', $file) or die "Unable to open '$file' for reading: $ +!"; my %freq; while (<$fh>) { tr/a-z//cd; ++$freq{$_} for split //; } while (%freq) { my ($small) = sort {$freq{$a} <=> $freq{$b}} keys %freq; seek $fh, 0, 0; my ($max, $word) = (-1, ''); while (<$fh>) { last if $max == keys %freq; next if index($_, $small) == -1; tr/a-z//cd; my %uniq = map {$_ => undef} split //; delete $uniq{$small}; my $cnt = grep defined $freq{$_}, keys %uniq; ($max, $word) = ($cnt, $_) if $cnt > $max; } print "$word\n"; delete @freq{split //, $word}; } __END__ photojournalism quarterbacked detoxifying blowzier aardvark

The algorithm is quite simple. Start with the rarest letter and look for words containing that letter that have the most unique letters not found so far. Wash-Rinse-Repeat.

Cheers - L~R

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://545167]
help
Chatterbox?
[marto]: something isn't quite right, getting the "woah cowboy.." message when trying to say good morning, it wasn't the last thing I said in the CB :/
[Corion]: marto: Hmm - now you didn't get the "whoa cowboy" ;)

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2017-02-28 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (399 votes). Check out past polls.