Task1
We take the intersection of arrays:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;
use Set::Scalar;
sub arrays_intersection{
my ($arr) = @_;
return () unless @$arr;
my $s = Set::Scalar->new($arr->[0]->@*);
map{$s *= Set::Scalar->new(@$_)} @{$arr}[1..$#$arr];
$s->elements
}
print show arrays_intersection([[1,2,3,4],[4,5,6,1],[4,2,1,3]]);
print show arrays_intersection([[1,0,2,3],[2,4,5]]);
print show arrays_intersection([[1,2,3],[4,5],[6]]);
Task2
We sort the array based on even and odd indices:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(mesh);
use Data::Show;
sub sort_even_odd{
grep {defined}
(mesh[sort{$a <=> $b}@{$_[0]}[grep{!($_%2)} 0..$#{$_[0]}]],
[sort{$b <=> $a}@{$_[0]}[grep{$_%2} 0..$#{$_[0]}]]);
}
print show sort_even_odd([4,1,2,3]);
print show sort_even_odd([3,1]);
print show sort_even_odd([5,3,2,1,4]);