aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-09-17 09:12:21 +0100
committerGitHub <noreply@github.com>2025-09-17 09:12:21 +0100
commit948fb0d43e8d06c7e3ace5cab80e35d03b1fa1c0 (patch)
tree467c9d0f098d11910ccdbd5f7a1da1fb3b3e154b
parent66c4aa9d6172c4120678b4aeb6c119af3edd7dcb (diff)
parent92f5d9ce22373b1f5bab85270e1046f2c19c1a7c (diff)
downloadperlweeklychallenge-club-948fb0d43e8d06c7e3ace5cab80e35d03b1fa1c0.tar.gz
perlweeklychallenge-club-948fb0d43e8d06c7e3ace5cab80e35d03b1fa1c0.tar.bz2
perlweeklychallenge-club-948fb0d43e8d06c7e3ace5cab80e35d03b1fa1c0.zip
Merge pull request #12695 from mattneleigh/pwc339
Pwc339
-rwxr-xr-xchallenge-339/mattneleigh/perl/ch-1.pl64
-rwxr-xr-xchallenge-339/mattneleigh/perl/ch-2.pl62
2 files changed, 126 insertions, 0 deletions
diff --git a/challenge-339/mattneleigh/perl/ch-1.pl b/challenge-339/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..471c5f73e9
--- /dev/null
+++ b/challenge-339/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @integer_lists = (
+ [ 5, 9, 3, 4, 6 ],
+ [ 1, -2, 3, -4 ],
+ [ -3, -1, -2, -4 ],
+ [ 10, 2, 0, 5, 1 ],
+ [ 7, 8, 9, 10, 10 ]
+);
+
+print("\n");
+foreach my $integer_list (@integer_lists){
+ printf(
+ "Input: \@ints = (%s)\nOutput: %d\n\n",
+ join(", ", @{$integer_list}),
+ maximum_pair_difference(@{$integer_list})
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a list of integers with at least four elements, determine the maximum
+# difference between the product of any two pairs in the list (i.e. two pairs
+# (a, b) and (c, d) such that the product difference (a * b) - (c * d) is the
+# greatest possible within the list)
+# Takes one argument:
+# * A list of integers to examine (e.g. (10, 2, 0, 5, 1) )
+# Returns:
+# * The maximum product difference of an two pairs within the list (e.g. 50)
+################################################################################
+sub maximum_pair_difference{
+ # Sort the list so the numbers farthest from zero
+ # are at the end of the list
+ my @ints = sort({ abs($a) <=> abs($b) } @ARG);
+
+ # Calculate and return the difference between
+ # the products of the two values farthest from
+ # zero and the two values closest to zero
+ return(
+ abs(
+ ($ints[-1] * $ints[-2])
+ -
+ ($ints[0] * $ints[1])
+ )
+ );
+
+}
+
+
+
diff --git a/challenge-339/mattneleigh/perl/ch-2.pl b/challenge-339/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..64a93a7c2c
--- /dev/null
+++ b/challenge-339/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @altitude_gain_sets = (
+ [ -5, 1, 5, -9, 2 ],
+ [ 10, 10, 10, -25 ],
+ [ 3, -4, 2, 5, -6, 1 ],
+ [ -1, -2, -3, -4 ],
+ [ -10, 15, 5 ]
+);
+
+print("\n");
+foreach my $altitude_gains (@altitude_gain_sets){
+ printf(
+ "Input: \@gain = (%s)\nOutput: %d\n\n",
+ join(", ", @{$altitude_gains}),
+ maximum_altitude(@{$altitude_gains})
+ );
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Given a list of altitude gains and losses, determine the maximum altitude
+# achieved, assuming a start at zero
+# Takes one argument:
+# * A list of altitude gains and losses (e.g. (3, -4, 2, 5, -6, 1) )
+# Returns:
+# * The maximum altitude achieved (e.g. 6)
+################################################################################
+sub maximum_altitude{
+
+ my $altitude = 0;
+ my $max = 0;
+
+ # Loop over all the gain values, apply each to
+ # the current altitude, and see if that yields
+ # a value bigger than any yet seen
+ foreach my $gain (@ARG){
+ $altitude += $gain;
+ $max = $altitude
+ if($altitude > $max);
+ }
+
+ return($max);
+
+}
+
+
+