[Israel.pm] What does this code do?

Shlomi Fish shlomif at iglu.org.il
Tue Apr 27 07:22:51 PDT 2004


Hi all!

I wrote the code below to verify the correctness of a solution to my sister's 
homework. Who can guess what it does?

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

use strict;

my $N = shift;

my $num_true_cases = 0;
sub helper
{
    my (@nums) = (@_);
    if (scalar(@nums) == $N)
    {
        my @bits = (0,0,0);
        for(@nums)
        {
            $bits[$_]=1;
        }
        if (! grep {!$_ } @bits)
        {
            $num_true_cases++;
        }
    }
    else
    {
        for my $next (0..2)
        {
            helper(@nums,$next);
        }
    }
}

helper();

print $num_true_cases, "\n";
>>>

Looking back at the code, there are a number of things I could have done 
differently, and I'll let you make some suggestions before I give mine.

Gabor, do you think such code understanding-riddles would be a desirable thing 
on the Perl Starter mailing-list?

Regards,

	Shlomi Fish



-- 

---------------------------------------------------------------------
Shlomi Fish      shlomif at iglu.org.il
Homepage:        http://shlomif.il.eu.org/

Quidquid latine dictum sit, altum viditur.
        [Whatever is said in Latin sounds profound.]



More information about the Perl mailing list