Attached is a test script which allows either zero or one as the
starting number:
tritest.pl <starting index> <scriptname>
e.g.:
perl tritest.pl 0 tri_0.pl
or
perl tritest.pl 1 tri_1.pl
My current score (which I hope to improve) is
0: 56
1: 54
Chris
Phil Carmody wrote:
> --- Jasper McCrea <[EMAIL PROTECTED]> wrote:
>
>>Michael W Thelen wrote:
>>
>>>On Wed, Jul 10, 2002 at 12:47:40PM +0100, Jasper McCrea wrote:
>>>
>>>>given a line number on the command line, print out that line
>>>
>>from
>>
>>>>Pascal's triangle. Single space separated numbers, nothing but
>>>
>>a \n at
>>
>>>>the end, you know the drill.
>>>
>>>Do the line numbers begin at 0 or 1? Is it like this?
>>>
>>>line 0: 1
>>>line 1: 1 1
>>>line 2: 1 2 1
>>>
>>
>>I think line numbering will begin at 1. 0 would be too odd. And it
>>would
>>add a few characters to my solution :)
>
>
> (1+1)^0 = 1
> (1+1)^1 = 1+1
> (1+1)^2 = 1+2+1
> (1+1)^3 = 1+3+3+1
> (1+1)^4 = 1+4+6+4+1
>
> Not odd at all.
>
> Phil
>
>>cheerio,
>>Jasper
>
>
>
> =====
#!/usr/bin/perl
use File::Slurp;
use strict;
$| = 1;
if (@ARGV == 0) {
die "Syntax: $0 0|1 [<file>]\n" .
" The first argument specifies whether to count from line 0 or line 1\n";
}
my $adjustment = shift;
my $filename = shift || 'tri.pl';
my @cases = (
"0",
<<EOF,
1
EOF
"1",
<<EOF,
1 1
EOF
"2",
<<EOF,
1 2 1
EOF
"3",
<<EOF,
1 3 3 1
EOF
"4",
<<EOF,
1 4 6 4 1
EOF
"5",
<<EOF,
1 5 10 10 5 1
EOF
"6",
<<EOF,
1 6 15 20 15 6 1
EOF
"7",
<<EOF,
1 7 21 35 35 21 7 1
EOF
"8",
<<EOF,
1 8 28 56 70 56 28 8 1
EOF
"9",
<<EOF,
1 9 36 84 126 126 84 36 9 1
EOF
"10",
<<EOF,
1 10 45 120 210 252 210 120 45 10 1
EOF
);
my $script = read_file $filename;
s/^\s*__END__.*//ms, s/\s*\z/\n/ for $script;
my $score = length($script) - 8;
print "$script\nscore: $score\n";
my $n;
for (my $i=0; $i < @cases; $i+=2)
{
my ($d, $want) = @cases[$i .. $i+1];
$d += $adjustment;
my $display = "---------- $d ----------\n";
#print $display; $display = '';
my $got = `$^X $filename $d 2>err.tmp`;
if(-s 'err.tmp')
{
print read_file 'err.tmp';
die "${display}printed to STDERR";
}
my $show = $got;
die "${display}failed, got \n$show wanted\n$want" unless $want eq $got;
printf "%3d: correctly found \n$want", ++$n;
}
print "\nPASSED\n\n";
print "$script\nscore: $score\n";
append_file 'run.log', "score: $score\n$script\n";