[Israel.pm] What does this code do?

David Baird david.baird at homemail.com
Wed Apr 28 04:28:28 PDT 2004

I waited a week. Here is my answer:

The program tests that for arrays of N elements, that all members are 0,
1, and 2, and that all three are present in the array. My alternative
code uses a hash instead of another array to test that all values exist.
I also placed the test for the correct number of elements at the top of
the subroutine, which I think makes for more clean code. I also go rid
of the extra assignment and some fluff code like scalar(). My code runs
no faster or slower the the original code. I am still thinking of how to
avoid the recursion. I may try an iterating subroutine. Give me some
time, if no one beats me to it.

#!/usr/bin/perl -w

use strict;

my $N = shift;
my $num_true_cases = 0;


print $num_true_cases, "\n";

sub helper
    if (@_ != $N) {
        helper(@_, $_) for (0..2);

    my %bits;
    $bits{$_} = 1 for @_;
    $num_true_cases++ if (keys %bits) == 3;

More information about the Perl mailing list