diff options
| author | Peter Meszaros <peter.meszaros@rollingwireless.com> | 2024-07-01 19:56:49 +0200 |
|---|---|---|
| committer | Peter Meszaros <peter.meszaros@rollingwireless.com> | 2024-07-01 19:56:49 +0200 |
| commit | f774e075a464bd01fe09da4bd9fe109f883aef42 (patch) | |
| tree | a5d79059360cbc6b10af407428c97c8e6637e2a2 | |
| parent | f18cb7a95e46b9ded70a2d1d932d0bb7b1772a67 (diff) | |
| download | perlweeklychallenge-club-f774e075a464bd01fe09da4bd9fe109f883aef42.tar.gz perlweeklychallenge-club-f774e075a464bd01fe09da4bd9fe109f883aef42.tar.bz2 perlweeklychallenge-club-f774e075a464bd01fe09da4bd9fe109f883aef42.zip | |
challenge-276
| -rwxr-xr-x | challenge-276/peter-meszaros/perl/ch-1.pl | 63 | ||||
| -rwxr-xr-x | challenge-276/peter-meszaros/perl/ch-2.pl | 67 |
2 files changed, 130 insertions, 0 deletions
diff --git a/challenge-276/peter-meszaros/perl/ch-1.pl b/challenge-276/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..211c0c05c3 --- /dev/null +++ b/challenge-276/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,63 @@ +#!/usr/bin/env perl +# +=head1 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. + +=head2 Example 1 + + Input: @hours = (12, 12, 30, 24, 24) + Output: 2 + + Pair 1: (12, 12) + Pair 2: (24, 24) + +=head2 Example 2 + + Input: @hours = (72, 48, 24, 5) + Output: 3 + + Pair 1: (72, 48) + Pair 2: (72, 24) + Pair 3: (48, 24) + +=head2 Example 3 + + Input: @hours = (12, 18, 24) + Output: 0 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; +use Math::Combinatorics; + +my $cases = [ + [[12, 12, 30, 24, 24], 2, 'Example 1'], + [[72, 48, 24, 5], 3, 'Example 2'], + [[12, 18, 24], 0, 'Example 3'], +]; + +sub complete_day +{ + my $l = shift; + + my $n = 0; + for my $pair (combine(2, @$l)) { + ++$n unless ($pair->[0] + $pair->[1]) % 24; + } + return $n; +} + +for (@$cases) { + is(complete_day($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-276/peter-meszaros/perl/ch-2.pl b/challenge-276/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..9264870b54 --- /dev/null +++ b/challenge-276/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl +# +=head1 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. + +=head2 Example 1 + + Input: @ints = (1, 2, 2, 4, 1, 5) + Ouput: 4 + + The maximum frequency is 2. + The elements 1 and 2 has the maximum frequency. + +=head2 Example 2 + + Input: @ints = (1, 2, 3, 4, 5) + Ouput: 5 + + The maximum frequency is 1. + The elements 1, 2, 3, 4 and 5 has the maximum frequency. + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[1, 2, 2, 4, 1, 5], 4, 'Example 1'], + [[1, 2, 3, 4, 5], 5, 'Example 2'], +]; + +sub maximum_frequency +{ + my $l = shift; + + my %h; + + $h{$_}++ for @$l; + my @l = sort {$h{$b} <=> $h{$a}} keys %h; + + my $freq = $h{$l[0]}; + + my $n; + for my $i (@l) { + if ($h{$i} == $freq) { + $n += $freq; + } else { + last; + } + } + + return $n; +} + +for (@$cases) { + is(maximum_frequency($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; + |
