Re: [QUIZ] Perl 'Easy' Quiz #2005-2

Rod Adams <[email protected]> Fri, 04 Feb 2005 13:50:14 -0600
Newsgroups gmane.comp.lang.perl.qotw.discuss
Message-ID <[email protected]>
Daniel Martin wrote:

>The tough part here was coming up with a way to check the answer that
>was not a spoiler of the problem.  I /think/ I succeeded, but this was
>certainly more difficult than a few attacks on the problem.
>
I likely would have settled for a predefined hash of ($input, $output) 
pairs, representing several common and edge cases.

btw, your test was very, very similar to one of my solutions (not 
inspired from you, though).


However, your test script inspired me to write my own (below).  Just run 
it w/ the other file as a command line arg.

-- Rod


#!/usr/bin/perl

%Tests = ('test.1.2.3' => 'test12.3',
      'test.123'   => 'test.123',
      '.test'      => '.test',
      'test.'      => 'test.',
      'test...123' => 'test.123',
      '.test.123'  => 'test.123',
      '.test.123.' => 'test123.');

do(shift @ARGV);

@INPUTS = sort keys %Tests;
@OUTPUTS = @Tests{@INPUTS};

print "Tests:\n";
for $i (1..@INPUTS) {
  printf("%2d %12s ==> %s\n", $i, $INPUTS[$i-1], $OUTPUTS[$i-1]);
}
print "\nSolutions:\n";

for $name (sort keys %::) {
  next unless *{$name}{CODE};
  printf("%2d %10s:", ++$solcount, $name);

  @errors = ();
  for $i_count (0..$#INPUTS) {
    $run_output = *{$name}{CODE}->($INPUTS[$i_count]);
    print ( $run_output eq $OUTPUTS[$i_count]
       ? ' +' : ' -');
    push @errors, "$INPUTS[$i_count] ==> $run_output\n"
    if $run_output ne $OUTPUTS[$i_count];
  }
  print "\n", @errors;
}

__END__