diff options
| -rw-r--r-- | challenge-276/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-276/bob-lied/perl/ch-1.pl | 64 | ||||
| -rw-r--r-- | challenge-276/bob-lied/perl/ch-2.pl | 64 |
3 files changed, 131 insertions, 3 deletions
diff --git a/challenge-276/bob-lied/README b/challenge-276/bob-lied/README index c6ba3198ae..3d141f3010 100644 --- a/challenge-276/bob-lied/README +++ b/challenge-276/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 275 by Bob Lied +Solutions to weekly challenge 276 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-275/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-275/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-276/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-276/bob-lied diff --git a/challenge-276/bob-lied/perl/ch-1.pl b/challenge-276/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..1fbb3d9798 --- /dev/null +++ b/challenge-276/bob-lied/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-1.pl Perl 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. +# Example 1 Input: @hours = (12, 12, 30, 24, 24) +# Output: 2 Pair 1: (12, 12) Pair 2: (24, 24) +# Example 2 Input: @hours = (72, 48, 24, 5) +# Output: 3 Pairs: (72, 48) (72, 24) (48, 24) +# Example 3 Input: @hours = (12, 18, 24) +# Output: 0 +#============================================================================= + +use v5.40; + +use Getopt::Long; +my $Verbose = false; +my $DoTest = false; +my $Benchmark = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark); +exit(!runTest()) if $DoTest; +exit( runBenchmark($Benchmark) ) if $Benchmark; + +say completeDay(@ARGV); + +sub completeDay(@hours) +{ + my $complete = 0; + while ( defined(my $first = shift @hours) ) + { + for my $second (@hours ) + { + $complete++ if ($first+$second)%24 == 0; + } + } + return $complete; +} + +sub runTest +{ + use Test2::V0; + + is( completeDay(12,12,30,24,24), 2, "Exsmple 1"); + is( completeDay(72,48,24, 5 ), 3, "Exsmple 2"); + is( completeDay(12,18,24 ), 0, "Exsmple 3"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese($repeat, { + label => sub { }, + }); +} diff --git a/challenge-276/bob-lied/perl/ch-2.pl b/challenge-276/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..b80eb1144e --- /dev/null +++ b/challenge-276/bob-lied/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-2.pl Perl Weekly Challenge 276 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. +# Example 1 Input: @ints = (1, 2, 2, 4, 1, 5) +# Ouput: 4 +# The maximum frequency is 2. +# The elements 1 and 2 have the maximum frequency. +# 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. +#============================================================================= + +use v5.40; + + +use Getopt::Long; +my $Verbose = false; +my $DoTest = false; +my $Benchmark = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark); +exit(!runTest()) if $DoTest; +exit( runBenchmark($Benchmark) ) if $Benchmark; + +say maxFreq(@ARGV); + +sub maxFreq(@ints) +{ + use List::MoreUtils qw/frequency/; + use List::Util qw/max sum/; + + my %freq = frequency @ints; + my $max = max values %freq; + + return sum grep { $_ == $max } values %freq; +} + +sub runTest +{ + use Test2::V0; + + is( maxFreq(1,2,2,4,1,5), 4, "Example 1"); + is( maxFreq(1,2,3,4,5 ), 5, "Example 2"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese($repeat, { + label => sub { }, + }); +} + |
