-
Notifications
You must be signed in to change notification settings - Fork 0
/
anamax.pl
executable file
·51 lines (36 loc) · 967 Bytes
/
anamax.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/local/bin/perl
# find groups of anagrams at the specified word length
# prints the largest groups found
use strict;
use warnings;
use File::Basename;
use Getopt::Long;
my $rc = GetOptions(
"wordlist=s" => \ (my $wordlist = 'wordlist'),
);
$rc && @ARGV == 1
or die "usage: anamax [--wordlist <wordlist>] <length>\n";
my $length = shift @ARGV;
if ($length =~ /\D/) {
die "<length> must be a whole number\n";
}
open(my $words_fh, '<', $wordlist) or die "Can't open $wordlist: $!\n";
my %count;
while (<$words_fh>) {
chomp;
next if length != $length;
my $canon = join '', sort split //;
push @{$count{$canon}}, $_;
}
close $words_fh;
print scalar(keys %count), " groups.\n";
my $i;
my $limit = 2;
for (sort { @{$count{$b}} <=> @{$count{$a}} ||
$count{$a}[0] cmp $count{$b}[0] } keys %count) {
last if @{$count{$_}} < $limit;
if ($limit == 2 and $i++ > 10) {
$limit = @{$count{$_}};
}
print "@{$count{$_}}\n";
}