[Israel.pm] What does this code do?
Moshe Kaminsky
kaminsky at math.huji.ac.il
Wed Apr 28 12:50:58 PDT 2004
* Shlomi Fish <shlomif at iglu.org.il> [27/04/04 17:36]:
> Hi all!
Hi,
>
> I wrote the code below to verify the correctness of a solution to my sister's
> homework. Who can guess what it does?
Much the same as:
perl -e '$N = shift; $num_true_cases=exp(log(3)*$N)-3*exp(log(2)*$N)+3;
printf "%d\n", $num_true_cases'
At least for natural numbers, that is.
>
> <<<
> #!/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.]
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://www.perl.org.il/mailman/listinfo/perl
>
--
I love deadlines. I like the whooshing sound they make as they fly by.
-- Douglas Adams
Moshe Kaminsky <kaminsky at math.huji.ac.il>
Home: 08-9456841
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://perl.org.il/pipermail/perl/attachments/20040428/8b12994d/attachment.pgp
More information about the Perl
mailing list