Skip to content

Instantly share code, notes, and snippets.

@cincodenada
Last active August 29, 2015 13:57
Show Gist options
  • Save cincodenada/9799899 to your computer and use it in GitHub Desktop.
Save cincodenada/9799899 to your computer and use it in GitHub Desktop.
Word deconstruction finder
#!/usr/bin/perl
use Data::Dumper;
use strict;
my $len;
my %tree;
while(<>) {
chomp;
if(/^[A-Za-z]+$/) {
$tree{length($_)}{$_} = [];
}
}
#Lots of weird single letters in my dicts...
$tree{1} = {
'a' => [],
'i' => [],
};
my @lengths = sort {$b <=> $a} keys %tree;
for my $length (@lengths) {
print "$length letters: " . scalar(keys %{$tree{$length}}) . "\n";
}
print check_word('startling');
die();
my $curlen = shift @lengths;
my @coolwords;
while(scalar(@coolwords) < 1) {
for my $word (keys %{$tree{$curlen}}) {
print "Checking $word...\n";
if(check_word($word, $len, 0)) {
push(@coolwords, $word);
}
}
}
print @coolwords;
sub check_word($$$) {
my $word = shift;
my $len = shift;
my $depth = shift;
if(!$len) { $len = length($word); }
if(exists $tree{$len}{$word}) {
if($len == 1) {
return 1;
} else {
my $continues = 0;
for(my $whichletter = 0; $whichletter < $len; $whichletter++) {
my $subword = substr($word, 0, $whichletter) . substr($word, $whichletter+1);
if(check_word($subword, $len-1, $depth+1)) {
print " "x$depth . "$word -> $subword\n";
push @{$tree{$len-1}{$subword}}, $word;
$continues = 1;
}
}
return $continues;
}
} else {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment