Task1
We look for the number which has a frequency equal to its value:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(max);
sub lucky_integer{
my %h;
$h{$_}++ foreach @{$_[0]};
max map{$h{$_} == $_ ? $_ : -1} keys %h
}
printf "%d\n",lucky_integer([2,2,3,4]);
printf "%d\n",lucky_integer([1,2,2,3,3,3]);
printf "%d\n",lucky_integer([1,1,1,3]);
Task2
We sort the elements according to their order in the second list:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;
sub relative_sort{
my (%ranks,%ranked);
$ranks{$_[1]->[$_]} = $_ foreach 0..$#{$_[1]};
$ranked{$_} = defined $ranks{$_} ? 1 : 0 foreach @{$_[0]};
sort{
($ranked{$b} <=> $ranked{$a}) ||
($ranks{$a} || 0) <=> ($ranks{$b} || 0) ||
$a <=> $b
} @{$_[0]}
}
print show relative_sort([2,3,9,3,1,4,6,7,2,8,5],[2,1,4,3,5,6]);
print show relative_sort([3,3,4,6,2,4,2,1,3],[1,3,2]);
print show relative_sort([3,0,5,0,2,1,4,1,1],[1,0,3,2]);