# 
# OK -- the lesson is... never submit code when you are barely able to sit
# due to the flu. Whatever else my brain is doing seems to be too much to
# allow for a simple cut and paste:


#!/usr/local/bin/perl -w

use strict;

## Read in the dictionary from STDIN to @words

chomp(my @words = (<>));

## Open outputs:

open (ANSWERS, ">answers") or die "Could not open answers file\n";
open (QUESTIONS, ">questions") or die "Could not open questions file\n";

## Convert all elements to lowercase and make dictionary unique

undef my %uniq;
@words=grep(!$uniq{$_}++,(map lc($_), @words));

## Initialize count and word hashes

my (%wordlist,%frags);

## subroutine getfour() gets all four-character fragments from the
## passed value and returns in an array. Only returns unique fragments.

sub getfour {
   undef my %ret;
   return grep(!$ret{$_}++,map substr($_[0],$_,4),0..length($_[0])-4);
}

## Program starts here -- cycle through all words in the dictionary.
## Use the fragment as the key to hashes for the count and corresponding
## first word. If a fragment is found that already is in the hash, then
## increment count. If it's new, store the word and set the initial fragment
## count.

foreach my $cword (@words) { if (length($cword)>3) {
   foreach my $frag (getfour($cword)) {
      if (! defined($frags{$frag}))
             {  $frags{$frag}=1; $wordlist{$frag} = $cword;}
	else {  $frags{$frag}++; undef $wordlist{$frag};}}}}

## go through the frag hash printing each fragment that only has one occurance
## along with the corresponding word.

foreach my $frag (sort keys %frags) {
   if ($frags{$frag} eq 1) {
	print ANSWERS $frag, "\n";
	print QUESTIONS $wordlist{$frag}, "\n";}}

close(ANSWERS);
close(QUESTIONS);

__END__


-- 
Douglas Palmer              |   Voice: 718-260-2291  Fax: 718-260-2338
SystemsManager, USDC-EDNY   |   rm -rf /bin/laden
225 Cadman Plaza East       |   Internet: palmer@nyed.uscourts.gov
Brooklyn, New York 11201    |   CCNA,CCNP
perl -e 'print $i=pack(h32,"a455443595350214e6f6e697d6f65737"),"\n";'


