The Weekly Challenge - 276


TASK #1: Complete Day
You are given an array of integers, @hours.

Write a script to return the number of pairs that forms a complete day.
A complete day is defined as a time duration that is an exact multiple of 24 hours.

#!/usr/bin/perl
use strict;
use warnings;

use Math::Combinatorics;

sub complete_day { 

    my $c = Math::Combinatorics->new ( count => 2, data => [@_], );

    my $count = 0; 
   
    while ( my @cmb = $c->next_combination ) {
		
        $count++ if ( ($cmb[0] + $cmb[1]) % 24 == 0 );
    
    }
  
    return ($count);
}

# Tests

my (@hours);

# Example 1
@hours = (12, 12, 30, 24, 24);
print(complete_day(@hours), "\n"); # Output: 2

# Example 2
@hours = (72, 48, 24, 5);
print(complete_day(@hours), "\n"); # Output: 3

# Example 3
@hours = (12, 18, 24);
print(complete_day(@hours), "\n"); # Output: 0

TASK #2: Maximum Frequency
You are given an array of positive integers, @ints.

Write a script to return the total number of elements in the given array which have the highest frequency.
#!/usr/bin/perl
use strict;
use warnings;

use Statistics::Frequency;
 
sub max_freq {

    my ($count, $f, %freq) = (0);

    $f = Statistics::Frequency->new( @_ );
    %freq = $f->frequencies;

    $count += $f->frequencies_max for ( grep { $_ == $f->frequencies_max } values %freq );
  
    return ($count);
}

# Tests

my (@ints);

# Example 1
@ints = (1, 2, 2, 4, 1, 5);
print(max_freq((@ints)), "\n"); # Output: 4

# Example 2
@ints = (1, 2, 3, 4, 5);
print(max_freq((@ints)), "\n"); # Output: 5

# Example 3
@ints = (1, 1, 3, 3, 3, 3);
print(max_freq((@ints)), "\n"); # Output: 4

# Example 4
@ints = (1, 2, 2, 4);
print(max_freq((@ints)), "\n"); # Output: 2