aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2025-01-27 14:18:50 +0000
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2025-01-27 14:18:50 +0000
commitf38cef5aeacfc827a81475e3c0872fa1f27dfb80 (patch)
tree74b85c96e2cbe070e5d70046f7703b7e1bfd2ccd
parent906bb690f8e1d091ac358c4ac77818bf879144c8 (diff)
downloadperlweeklychallenge-club-f38cef5aeacfc827a81475e3c0872fa1f27dfb80.tar.gz
perlweeklychallenge-club-f38cef5aeacfc827a81475e3c0872fa1f27dfb80.tar.bz2
perlweeklychallenge-club-f38cef5aeacfc827a81475e3c0872fa1f27dfb80.zip
Week 306 - Odd game
-rw-r--r--challenge-306/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-306/peter-campbell-smith/perl/ch-1.pl32
-rwxr-xr-xchallenge-306/peter-campbell-smith/perl/ch-2.pl38
3 files changed, 71 insertions, 0 deletions
diff --git a/challenge-306/peter-campbell-smith/blog.txt b/challenge-306/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..991e574038
--- /dev/null
+++ b/challenge-306/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/306
diff --git a/challenge-306/peter-campbell-smith/perl/ch-1.pl b/challenge-306/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..7d7c79b005
--- /dev/null
+++ b/challenge-306/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-12-27
+use utf8; # Week 306 - task 1 - Odd sum
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+odd_sum(2, 5, 3, 6, 4);
+odd_sum(99);
+odd_sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
+
+sub odd_sum {
+
+ my (@ints, $sum, $start, $end);
+
+ # initialise
+ @ints = @_;
+ $sum = 0;
+
+ # loop over start and end points
+ for $start (0 .. $#ints) {
+ for ($end = $start; $end <= $#ints; $end += 2) {
+ $sum += $ints[$_] for $start .. $end;
+ }
+ }
+
+ # report
+ say qq[\nInput: \@ints = (] . join(', ', @ints) . qq[)];
+ say qq[Output: $sum];
+}
diff --git a/challenge-306/peter-campbell-smith/perl/ch-2.pl b/challenge-306/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..128bd50ec0
--- /dev/null
+++ b/challenge-306/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-12-27
+use utf8; # Week 306 - task 2 - Last element
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+last_element(3, 8, 5, 2, 9, 2);
+last_element(3, 2, 5);
+last_element(43, 21, 56, 86, 2, 69, 10, 43, 77, 30);
+
+sub last_element {
+
+ my (@ints, $x);
+
+ # initialise
+ @ints = @_;
+ say qq[\nInput: \@ints = (] . join(', ', @_) . ')';
+
+ # loop until 0 or 1 element left
+ while ($#ints > 0) {
+ @ints = sort {$b <=> $a} @ints;
+
+ # if x == y
+ if ($ints[0] == $ints[1]) {
+ shift @ints;
+ shift @ints;
+
+ # if x != y
+ } else {
+ $x = shift @ints;
+ $ints[0] = $x - $ints[0];
+ }
+ }
+ say qq[Output: ] . ($ints[0] or 0);
+}