1
0
Fork 0
This repository has been archived on 2020-09-12. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
add-a-gram/addagram.pl
2004-06-10 17:35:24 +00:00

33 lines
742 B
Perl
Executable file

#!/usr/bin/perl -w
# Usage: addagram.pl < /net/doc/random-facts/WORD.LST
while(<>) {
chomp;
my $letters = join("", sort split( //, $_ ));
push(@{$got{$letters}}, $_);
}
$maxlen = 0;
foreach $word (sort {length($b) <=> length($a)} keys %got) {
exit if length($word) < $maxlen;
if (&deconstruct($word)) {
for (my $l = $maxlen = length($word); $l>=3; $l--) {
printf("%2d: %s\n", $l, join(',', @{$got{$solution[$l]}}));
}
print "\n";
}
}
sub deconstruct {
my($word) = @_;
return 0 if !$got{$word};
my $len = length($word);
$solution[$len] = $word;
return 1 if $len <= 3;
for (my $l=0; $l<$len; $l++) {
return 1 if &deconstruct(substr($word, 0, $l) . substr($word, $l+1));
}
return 0;
}