Task1
If the sum of any of the pairs is divisible by 24, we increment the count value:
#!usr/bin/env perl
use strict;
use warnings;
sub complete_day{
my ($arr) = @_;
my $count = 0;
foreach my $i(0..$#$arr-1){
foreach my $j($i+1..$#$arr){
$count++ if(($arr->[$i] + $arr->[$j]) % 24 == 0)
}
}
$count
}
printf "%d\n",complete_day([12,12,30,24,24]);
printf "%d\n",complete_day([72,48,24,55]);
printf "%d\n",complete_day([12,18,24]);
Task2
We count the values, and sum the ones which have a weight equal to the max weight of the elements:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(max sum0);
sub maximum_frequency{
my %h;
$h{$_}++ foreach @{$_[0]};
my $max = max values %h;
sum0 grep {$_ if $_ == $max} values %h
}
printf "%d\n",maximum_frequency([1,2,2,4,1,5]);
printf "%d\n",maximum_frequency([1,2,3,4,5]);
No comments:
Post a Comment