diff options
| author | robbie-hatley <Robbie.Hatley@gmail.com> | 2024-06-30 19:36:31 -0700 |
|---|---|---|
| committer | robbie-hatley <Robbie.Hatley@gmail.com> | 2024-06-30 19:36:31 -0700 |
| commit | fd0dfc57df65fef42db64d2c57eadd5962e2c006 (patch) | |
| tree | 57c866b7a7850b1644ec71e3f4fdefc7b0a1e1a4 | |
| parent | f18cb7a95e46b9ded70a2d1d932d0bb7b1772a67 (diff) | |
| download | perlweeklychallenge-club-fd0dfc57df65fef42db64d2c57eadd5962e2c006.tar.gz perlweeklychallenge-club-fd0dfc57df65fef42db64d2c57eadd5962e2c006.tar.bz2 perlweeklychallenge-club-fd0dfc57df65fef42db64d2c57eadd5962e2c006.zip | |
Robbie Hatley's solutions, in Perl, for The Weekly Challenge #276.
| -rw-r--r-- | challenge-276/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-276/robbie-hatley/perl/ch-1.pl | 90 | ||||
| -rwxr-xr-x | challenge-276/robbie-hatley/perl/ch-2.pl | 87 |
3 files changed, 178 insertions, 0 deletions
diff --git a/challenge-276/robbie-hatley/blog.txt b/challenge-276/robbie-hatley/blog.txt new file mode 100644 index 0000000000..a6e31e786d --- /dev/null +++ b/challenge-276/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2024/06/robbie-hatleys-solutions-to-weekly_30.html
\ No newline at end of file diff --git a/challenge-276/robbie-hatley/perl/ch-1.pl b/challenge-276/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..2b66f7f281 --- /dev/null +++ b/challenge-276/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,90 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 276-1, +written by Robbie Hatley on Sun Jun 30, 2024. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 276-1: Complete Day +Submitted by: Mohammad Sajid Anwar +Given an array of integers, 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: +[12, 12, 30, 24, 24], +Expected output: 2 +Pair 1: (12, 12) +Pair 2: (24, 24) + +Example 2 input: +[72, 48, 24, 5], +Expected output: 3 +Pair 1: (72, 48) +Pair 2: (72, 24) +Pair 3: (48, 24) + +Example 3 input: +[12, 18, 24], +Expected output: 0 + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +This is just a matter of using nested 3-part loops to avoid duplicating pairs, then seeing which pairs add up +to an integer x such that 0 == x%24. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of arrays of integers, in proper Perl syntax, like so: +./ch-1.pl '([3, -17, 24, -31, 0, 21],[5, 19, 0, 347, -82])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, VARIABLES, AND SUBS: + +use v5.38; +$"=', '; +sub complete_days (@times) { + my $cds = 0; + for ( my $i = 0 ; $i <= $#times - 1 ; ++$i ) { + for ( my $j = $i + 1 ; $j <= $#times - 0 ; ++$j ) { + 0 == ($times[$i]+$times[$j])%24 and ++$cds; + } + } + $cds; +} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1 input: + [12, 12, 30, 24, 24], + # Expected output: 2 + + # Example 2 input: + [72, 48, 24, 5], + # Expected output: 3 + + # Example 3 input: + [12, 18, 24], + # Expected output: 0 +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +for my $aref (@arrays) { + say ''; + my @times = @$aref; + say "Times = (@times)"; + my $cds = complete_days(@times); + say "Complete days = $cds"; +} diff --git a/challenge-276/robbie-hatley/perl/ch-2.pl b/challenge-276/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..fb99d29e1f --- /dev/null +++ b/challenge-276/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,87 @@ +#!/usr/bin/env -S perl -CSDA + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 276-2, +written by Robbie Hatley on Sun Jun 30, 2024. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 276-2: Maximum Frequency +Submitted by: Mohammad Sajid Anwar +Given an array of positive integers, write a script to return +the total number of elements in the given array which have the +highest frequency. + +Example 1 input: +[1, 2, 2, 4, 1, 5], +Expected output: 4 +The maximum frequency is 2. +The elements 1 and 2 has the maximum frequency. + +Example 2 input: +[1, 2, 3, 4, 5], +Expected output: 5 +The maximum frequency is 1. +The elements 1, 2, 3, 4 and 5 has the maximum frequency. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +To solve this, I used "frequency" from "List::MoreUtils" to get a flat list of item->frequency pairs, then I +used "pairs" from "List::Util" to convert the flat list into a list of pairs, then I used +"sort {$b->[1]<=>$a->[1]}" to put the pairs in inverse order of frequency. I then tallied-up the +frequencies of the items having maximum frequency. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of arrays of positive integers, in proper Perl syntax, like so: +./ch-2.pl '([83, 7, 16, 5, 7], [3, 7, 8, 7, 16, 7, 54, 7], [5, 4, 5, 4, 5, 4, 3, 3])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.38; + use List::MoreUtils 'frequency'; + use List::Util 'pairs'; + sub total_items_with_maximum_frequency (@items) { + my @pairs = pairs frequency @items; + my @sorted = sort {$b->[1]<=>$a->[1]} @pairs; + my $max_freq = $sorted[0]->[1]; + my $total_max_freq = 0; + for my $pair (@sorted) { + last if $pair->[1] != $max_freq; + $total_max_freq += $max_freq; + } + $total_max_freq; + } + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1 input: + [1, 2, 2, 4, 1, 5], + # Expected output: 4 + + # Example 2 input: + [1, 2, 3, 4, 5], + # Expected output: 5 +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $aref (@arrays) { + say ''; + my @items = @$aref; + say "Items = @items"; + my $tiwmf = total_items_with_maximum_frequency(@items); + say "Total items with maximum frequency = $tiwmf"; +} |
