aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-339/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-339/peter-campbell-smith/perl/ch-1.pl47
-rwxr-xr-xchallenge-339/peter-campbell-smith/perl/ch-2.pl37
3 files changed, 85 insertions, 0 deletions
diff --git a/challenge-339/peter-campbell-smith/blog.txt b/challenge-339/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..d08186a50e
--- /dev/null
+++ b/challenge-339/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/339
diff --git a/challenge-339/peter-campbell-smith/perl/ch-1.pl b/challenge-339/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..ae55bc08c0
--- /dev/null
+++ b/challenge-339/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-09-15
+use utf8; # Week 339 - task 1 - Max diff
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+use Algorithm::Combinatorics 'variations';
+
+max_diff(5, 9, 3, 4, 6);
+max_diff(1, -2, 3, -4);
+max_diff(-3, -1, -2, -4);
+max_diff(10, 2, 0, 5, 1);
+max_diff(7, 8, 9, 10, 10);
+max_diff(0, 0, 0, 0, 0);
+max_diff(5, 5, 5, 5, 5);
+max_diff(-5, -5, -5, -5, -5);
+
+# 50 random numbers in [-250 .. +250]
+my @ints;
+push @ints, int(rand(501)) - 250 for 1 .. 50;
+max_diff(@ints);
+
+sub max_diff {
+
+ my ($best, $iter, $c, $diff, $pairs);
+
+ # initialise
+ $best = -1;
+ $iter = variations(\@_, 4);
+
+ # iterate over all variations
+ while ($c = $iter->next) {
+
+ # is this one the best so far?
+ $diff = abs($c->[0] * $c->[1] - $c->[2] * $c->[3]);
+ if ($diff > $best) {
+ $best = $diff;
+ $pairs = qq[($c->[0], $c->[1]), ($c->[2], $c->[3])];
+ }
+ }
+
+ say qq[\nInput: (] . join(', ', @_) . ')';
+ say qq[Output: $best\n $pairs];
+} \ No newline at end of file
diff --git a/challenge-339/peter-campbell-smith/perl/ch-2.pl b/challenge-339/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..aa8feb2c82
--- /dev/null
+++ b/challenge-339/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-09-15
+use utf8; # Week 339 - task 2 - Peak point
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+peak_point(-5, 1, 5, -9, 2);
+peak_point(10, 10, 10, -25);
+peak_point(3, -4, 2, 5, -6, 1);
+peak_point(-1, -2, -3, -4);
+peak_point(-10, 15, 5);
+
+my @steps;
+push @steps, int(rand(21)) - 10 for 1 .. 100;
+peak_point(@steps);
+
+sub peak_point {
+
+ my ($height, $highest, $climb);
+
+ # initialise
+ $height = 0;
+ $highest = 0;
+
+ # start climbing
+ for $climb (@_) {
+ $height += $climb;
+ $highest = $height if $height > $highest;
+ }
+
+ say qq[\nInput: (] . join(', ', @_) . ')';
+ say qq[Output: $highest];
+}