#! perl -w
# This is a fairly straightforward solution. I have a map from
# four-letter questions to the list of answers. Of course, only
# single answers are correct.
use strict;
# Open the output.
open QH, "> questions" or die "Cannot open questions: $!\n";
open AH, "> answers" or die "Cannot open answers: $!\n";
# The key of %map is the four-letter "question", while the value of
# %map is a list of "answers". Only single answers are correct.
my %map;
while (<>)
{
chomp;
# I've decided that the key is case insensitive, but the word is
# not. Therefore, iela is not a question, since it has two answers,
# Dixieland and dixieland.
my @letters = split //, lc;
while (scalar @letters >= 4)
{
my $key = join('', @letters[0 .. 3]);
shift @letters;
# Only want the key if it all letters.
next unless $key =~ /[a-z]{4}/;
push @{$map{$key}}, $_;
}
}
while (my ($question, $answers) = each %map)
{
next unless @$answers == 1;
print QH "$question\n";
print AH "@$answers\n";
}
# Close the output.
close QH or die "Cannot close questions: $!\n";
close AH or die "Cannot close answers: $!\n";
# end file 08.easy
|