Task1
We check if the coordinates form a straight line:
#!/usr/bin/env perl
use strict;
use warnings;
sub straight_line{
my ($m) = @_;
my ($x1,$y1,$x2,$y2) = ($m->[0][0],$m->[0][1],$m->[1][0],$m->[1][1]);
for(my $i = 2; $i < @$m; ++$i) {
my ($x,$y) = ($m->[$i][0],$m->[$i][1]);
return 0 if(($x - $x1) * ($y2 - $y1) != ($y - $y1) * ($x2 - $x1))
}
1
}
printf "%d\n",straight_line([[2,1],[2,3],[2,5]]);
printf "%d\n",straight_line([[1,4],[3,4],[10,4]]);
printf "%d\n",straight_line([[0,0],[1,1],[2,3]]);
printf "%d\n",straight_line([[1,1],[1,1],[1,1]]);
printf "%d\n",straight_line([[1000000,1000000],
[2000000,2000000],
[3000000,3000000]]);
Task2
We duplicate each zero twice until we reach array size and stop:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;
sub duplicate_zeros{
my ($arr) = @_;
my ($i,@stk) = (0);
while($i < @$arr && @stk != @$arr){
if($arr->[$i] == 0 && (@stk+1 < @$arr)) {
push @stk,0 for 0..1;
$i++
} else {
push @stk,$arr->[$i++]
}
}
@stk
}
print show duplicate_zeros([1,0,2,3,0,4,5,0]);
print show duplicate_zeros([1,2,3]);
print show duplicate_zeros([1,2,3,0]);
print show duplicate_zeros([0,0,1,2]);
print show duplicate_zeros([1,2,0,3,4]);
No comments:
Post a Comment