aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-06 01:52:39 +0000
committerGitHub <noreply@github.com>2023-03-06 01:52:39 +0000
commit3cde7a484583d8898079b19f0bb5bfd760cc5dc9 (patch)
treedc39c74e438a500149b13f0aca92c8681df84e91
parent1f4b82890a4e63ab63a49926604cb6ef112ae21c (diff)
parente68e038e3a54a807c4c795685c7ca493b8e986d9 (diff)
downloadperlweeklychallenge-club-3cde7a484583d8898079b19f0bb5bfd760cc5dc9.tar.gz
perlweeklychallenge-club-3cde7a484583d8898079b19f0bb5bfd760cc5dc9.tar.bz2
perlweeklychallenge-club-3cde7a484583d8898079b19f0bb5bfd760cc5dc9.zip
Merge pull request #7669 from mattneleigh/pwc206
new file: challenge-206/mattneleigh/perl/ch-1.pl
-rwxr-xr-xchallenge-206/mattneleigh/perl/ch-1.pl121
-rwxr-xr-xchallenge-206/mattneleigh/perl/ch-2.pl90
2 files changed, 211 insertions, 0 deletions
diff --git a/challenge-206/mattneleigh/perl/ch-1.pl b/challenge-206/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..aae8f60a73
--- /dev/null
+++ b/challenge-206/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,121 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+use List::Util qw(min);
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @time_lists = (
+ [ "00:00", "23:55", "20:00" ],
+ [ "01:01", "00:50", "00:57" ],
+ [ "10:10", "09:30", "09:00", "09:55" ]
+);
+
+print("\n");
+foreach my $time_list (@time_lists){
+ printf(
+ "Input: \@time = (%s)\nOutput: %d\n\n",
+ join(", ", map("\"$_\"", @{$time_list})),
+ shortest_time_interval_in_list(@{$time_list})
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Calculate the shortest interval between any two times in HH:MM format within
+# a list, in minutes. Note that the shortest interval may pass through
+# midnight (00:00); if so, this function does not report that this was the
+# case. The order in which the times appear in the list does not affect the
+# calculation, and the shortest time may not be between adjacent members of
+# the list; this function does not report between which times the calculated
+# interval occurred.
+# Takes one argument:
+# * The list of times to examine
+# Returns:
+# * The shortest interval between the various times n the list, in minutes; see
+# notes above
+################################################################################
+sub shortest_time_interval_in_list{
+
+ return(
+ # min() is from List::Util
+ min(
+ # Get a list of intervals between all times
+ # in the argument list- do this with nested
+ # calls to map() in which $i and the inner
+ # $_ will, as array indices, refer to all
+ # pairs in the list
+ map(
+ {
+ # Capture the outer $_
+ my $i = $_;
+
+ map(
+ calculate_time_interval_pair(
+ $ARG[$i],
+ $ARG[$_]
+ ),
+ ($i + 1) .. $#ARG
+ );
+ }
+ 0 .. ($#ARG - 1)
+ )
+ )
+ );
+
+}
+
+
+
+################################################################################
+# Calculate the shortest inverval, in minutes, between two times in HH:MM
+# format. Note that the shortest interval may pass through midnight (00:00);
+# if so, this function does not report that this was the case. The order in
+# which the arguments appear does not affect the calculation.
+# Takes two arguments:
+# * The first time to examine
+# * The second time to examine
+# Returns:
+# * The time interval between the specified times, in minutes; see notes above
+################################################################################
+sub calculate_time_interval_pair{
+ my $a = shift();
+ my $b = shift();
+
+ # Convert HH:MM format to minutes since
+ # midnight
+ $a =~ m/^(\d+):(\d+)$/;
+ $a = 60 * $1 + $2;
+ $b =~ m/^(\d+):(\d+)$/;
+ $b = 60 * $1 + $2;
+
+ # Make sure $b is larger than $a
+ ($b, $a) = ($a, $b)
+ if($a > $b);
+
+ # Return the minimum of the two possible
+ # distances between the two times. 1440
+ # is the number of minutes in a day;
+ # min() is from List::Util
+ return(
+ min(
+ ($b - $a),
+ (1440 - $b + $a)
+ )
+ );
+
+}
+
+
+
diff --git a/challenge-206/mattneleigh/perl/ch-2.pl b/challenge-206/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..50753d2ece
--- /dev/null
+++ b/challenge-206/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,90 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+use List::Util qw(min max);
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @lists = (
+ [ 1, 2, 3, 4 ],
+ [ 0, 2, 1, 3 ]
+);
+
+print("\n");
+foreach my $list (@lists){
+ printf(
+ "Input: \@array = (%s)\nOutput: %d\n\n",
+ join(", ", @{$list}),
+ max_sum_of_pair_mins(@{$list})
+ );
+}
+
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Determine the maximum of the sums of the minimums of each set of pairs within
+# an array holding an even quantity of numbers
+# Takes one argument:
+# * An array of numbers with an even number of members
+# Returns on success:
+# * The maximum of the sums of the minimum values in each pair within the array
+# Returns on error:
+# * undef if the array has an odd number of members
+################################################################################
+sub max_sum_of_pair_mins{
+
+ my @pairs;
+
+ return(undef)
+ if(scalar(@ARG) % 2);
+
+ # Grab a list of all possible pairs within
+ # the given array; do this with nested calls
+ # to map() in which $i and the inner $_ will,
+ # as array indices, refer to all pairs
+ @pairs = map(
+ {
+ # Capture the outer $_
+ my $i = $_;
+
+ map(
+ [ $ARG[$i], $ARG[$_] ],
+ ($i + 1) .. $#ARG
+ );
+ }
+ 0 .. ($#ARG - 1)
+ );
+
+ # max() and min() are from List::Util
+ return(
+ # (2) ...then find the maximum value in
+ # that list
+ max(
+ # (1) Generate a list of the sums of the
+ # minima from each pair of pairs...
+ map(
+ (
+ min(@{$pairs[$_]})
+ +
+ min(@{$pairs[$#pairs - $_]})
+ ),
+ 0 .. ($#pairs / 2)
+ )
+ )
+ );
+
+}
+
+
+