Task1
We sort the elements according to order array:
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 5;
sub finishing_order{
my %h;
$h{$_} = 1 foreach @{$_[1]};
[grep {$h{$_}} @{$_[0]}];
}
is_deeply finishing_order([3,1,4,2,5],[1,2,4]),[1,4,2],'Example 1';
is_deeply finishing_order([5,4,3,2,1],[1,3,5]),[5,3,1],'Example 2';
is_deeply finishing_order([2,4,1,3],[1,2,3,4]),[2,4,1,3],'Example 3';
is_deeply finishing_order([4,1,3,2],[3]),[3],'Example 4';
is_deeply finishing_order([1,3,2,6,5,4],[1,2,3]),[1,3,2],'Example 5';
Task2
We check if array is trionic:
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 5;
sub trionic_array{
my ($arr) = @_;
my ($p,$n) = (0,scalar @$arr);
$p++ while($p < $n - 2 && $arr->[$p] < $arr->[$p+1]);
return '' if $p == 0;
my $q = $p;
$q++ while($q < $n-1 && $arr->[$q] > $arr->[$q+1]);
return '' if $q == $p || $q == $n-1;
$q++ while($q < $n-1 && $arr->[$q] < $arr->[$q+1]);
$q == $n-1
}
is trionic_array([1,3,5,4,2,6,8]),1,'Example 1';
is trionic_array([1,4,4,2,1,3,5]),'','Example 2';
is trionic_array([2,10,3,1,9]),1,'Example 3';
is trionic_array([1,2,3,4,3,2,1]),'','Example 4';
is trionic_array([1,3,6,4,2,7,8,4]),'','Example 5';
No comments:
Post a Comment