Skip to content

Instantly share code, notes, and snippets.

@emacdona
Created March 3, 2014 21:35
Show Gist options
  • Save emacdona/9335096 to your computer and use it in GitHub Desktop.
Save emacdona/9335096 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
my $dictionary = "/usr/share/dict/words";
open DICTIONARY, "<$dictionary" or die "$!";
my $map = {};
while(my $word = <DICTIONARY>){
chomp $word;
if(length($word) > 3){
$word = lc $word;
my @word = split(//, $word);
my ($firstLetter, $lastLetter) = @word[0,-1];
my $key = join '', sort {$a cmp $b} @word;
$map->{$key}->{$firstLetter}->{$lastLetter}->{$word}++;
}
}
for my $key (keys (%$map)){
for my $firstLetter (keys %{$map->{$key}}) {
for my $lastLetter (keys %{$map->{$key}->{$firstLetter}}) {
if( scalar( keys %{$map->{$key}->{$firstLetter}->{$lastLetter}} ) > 1){
print join(",", keys(%{$map->{$key}->{$firstLetter}->{$lastLetter}}));
print "\n";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment