in reply to Golfing Array_Pad

I haven't examined the code past the fact that your implementation and array_pad don't match up
#Returns a copy of the input padded to size specified by pad_size with + value pad_value. #If pad_size is positive then the array is padded on the right, if it' +s negative then on the left. #If the absolute value of pad_size is less than or equal to the length + of the input then no padding takes place. #int pad_size, mixed pad_value, array input sub array_pad{ my($pad_size, $pad_value, @array) = @_; #this is just to get the absolute value of the pad_size my $temp = $pad_size; if($pad_size < 0){ $temp = $temp * -1; }#if #first let's see if the pad_size is less than or equal to the arra +y size, return 0 no padding my $input_size = @array; if($temp <= $input_size){ return 0; }#if #since it didn't return 0, time to pad #if the pad_size is < 0, pad on the left, else the right if($pad_size < 0){ my @retval; $temp = $temp - $input_size; for(my $i=0;$i<$temp;$i++){ unshift(@array, $pad_value); }#for return @array; }#if else{ $temp = $pad_size - $input_size; for(my $i=0;$i<$temp;$i++){ push(@array, $pad_value); }#for return @array; }#else }#array_pad sub p{my($i,$z,@f)=@_;abs$i>@f&&$i<0?@f=(($z) x abs($i+@f),@f):(@f[@f. +.$i-1]=($z)x($i-@f));@f} use Test::More tests => 4; my @x = 1 .. 5; is_deeply([p( 3, 'x', @x)], [array_pad( 3, 'x', @x)]); is_deeply([p(-3, 'x', @x)], [array_pad(-3, 'x', @x)]); is_deeply([p( 7, 'x', @x)], [array_pad( 7, 'x', @x)]); is_deeply([p(-7, 'x', @x)], [array_pad(-7, 'x', @x)]); __END__ 1..4 not ok 1 # Failed test (golfing.array_pad.pl at line 43) # Structures begin differing at: # $got->[0] = '1' # $expected->[0] = '0' not ok 2 # Failed test (golfing.array_pad.pl at line 44) # Structures begin differing at: # $got->[0] = '1' # $expected->[0] = '0' ok 3 ok 4 # Looks like you failed 2 tests of 4.

MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
** The third rule of perl club is a statement of fact: pod is sexy.

Replies are listed 'Best First'.
Re^2: Golfing Array_Pad
by BUU (Prior) on Jan 23, 2005 at 05:55 UTC
    Congratulations, you've found one of the (I'm sure many) bugs in array_pad, but I don't feel that the bugs in array_pad are really relevant to the golfing contest, do you?
      And here I thought it might've been a bug in your implementation. If you knew array_pad was flawed, you should've mentioned it. I'm sure I wouldn't have been the first to think that the reference implementation would be correct and could be used to verify results.

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.