# On Wed, Dec 11, 2002 at 01:25:02PM -0500, Mark Jason Dominus wrote:
# >Write a Perl program which, given a dictionary, generates two output
# >files, 'questions' and 'answers'.
# 
# The main "clever" bit here, for me, was finding a means of extracting
# all possible four-letter substrings from the source word. Using m/..../g
# was the obvious step, but stripping leading characters off the word
# feels clumsy. I look forward to seeing better solutions...
# 
# Not having to deduplicate the input list would make the code somewhat
# less memory-hungry. For a big dictionary, I'd probably use tied hashes -
# though this runs quite happily on a 400MHz PII with 128M of memory,
# producing output from Web2 in about 33 seconds.
# 
# Moving the exists check to after the non-alpha and length checks gives a
# 0.4% time saving.
# 
# My style, as you will note, is more C than "native Perl".
# 
#! /usr/bin/perl -w

use strict;

my %word;
my %stub;

while (<>) {
  chomp;
  my $word=lc($_);
  unless ($word =~ /[^a-z]/ || length($word)<4 || exists $word{$word}) {
    $word{$word}=1;
    my @frag= $word =~ /..../g;
    my $t=$word;
    foreach (1..3) {
      $t =~ s/^.//;
      push @frag, $t =~ /..../g;
    }
    foreach (@frag) {
      if (exists $stub{$_}) {
        $stub{$_}=0;
      } else {
        $stub{$_}=$word;
      }
    }
  }
}

open Q,'>questions';
open A,'>answers';
foreach (sort keys %stub) {
  if ($stub{$_}) {
    print Q "$_\n";
    print A "$stub{$_}\n";
  }
}
close Q;
close A;
