use Benchmark;
$letters = 'chunzcii';
$lhash{$_}++ for split //, $letters;
$lcount = length($letters);
$t0 = new Benchmark;
open ($handle, 'dictionary.dat');
while (<$handle>) {
chomp; $w++ if scrabble($_);
$c++;
}
close ($handle);
$t1 = new Benchmark;
$td = timediff($t1, $t0);
print "the code took:",timestr($td),"\n";
print "$w matches from $c words";
sub scrabble {
return 0 if length($_[0]) > $lcount;
my %wlhash;
$wlhash{$_}++ for split //, $_[0];
for (keys %wlhash) {
return 0 if $lhash{$_} < $wlhash{$_} &&
($nf += $wlhash{$_} - $lhash{$_}) > $blanks;
}
return 1;
}
####
use Benchmark;
$letters = 'chunzcii';
$sorted = join '', sort split //, $letters;
$lcount = length($letters);
$t0 = new Benchmark;
open ($handle, 'dictionary.dat');
while (<$handle>) {
chomp; $w++ if scrabble($_);
$c++;
}
close ($handle);
$t1 = new Benchmark;
$td = timediff($t1, $t0);
print "the code took:",timestr($td),"\n";
print "$w matches from $c words";
sub scrabble {
return 0 if length($_[0]) > $lcount;
$p = 0;
for (sort split //, $_[0]) {
return 0 if !($p = index($sorted, $_, $p) + 1);
}
return 1;
}
####
use Benchmark;
$letters = 'chuncii_';
$lcount = length($letters);
while ($letters =~ /[^a-z]/) {
$letters =~ s/[^a-z]//;
$blanks++;
}
$lhash{$_}++ for split //, $letters;
$t0 = new Benchmark;
open ($handle, 'dictionary.dat');
while (<$handle>) {
chomp; $w++ if scrabble($_);
$c++;
}
close ($handle);
$t1 = new Benchmark;
$td = timediff($t1, $t0);
print "the code took:",timestr($td),"\n";
print "$w matches from $c words";
sub scrabble {
return 0 if length($_[0]) > $lcount;
my %wlhash;
$wlhash{$_}++ for split //, $_[0];
$nf = 0;
for (keys %wlhash) {
return 0 if $lhash{$_} < $wlhash{$_} &&
($nf += $wlhash{$_} - $lhash{$_}) > $blanks;
}
return 1;
}
####
$letters = 'chuncii_';
$lcount = length($letters);
while ($letters =~ /[^a-z]/) {
$letters =~ s/[^a-z]//;
$blanks++;
}
$lhash{$_}++ for split //, $letters;
open ($handle, 'dictionary2.dat');
while (<$handle>) {
chomp; push @matches, $_ if scrabble($_);
}
close ($handle);
print join "\n", sort { length($b) <=> length($a) || $a cmp $b } @matches;
sub scrabble {
return 0 if length($_[0]) > $lcount;
my %wlhash;
$wlhash{$_}++ for split //, $_[0];
$nf = 0;
for (keys %wlhash) {
return 0 if $lhash{$_} < $wlhash{$_} &&
($nf += $wlhash{$_} - $lhash{$_}) > $blanks;
}
return 1;
}
####
zucchini
acacia
couch
lunch
kick
zinc
cut
did
hen
hit
ice
kin
nip
sun
ugh
pi
a