aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-261/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-261/peter-campbell-smith/perl/ch-1.pl37
-rwxr-xr-xchallenge-261/peter-campbell-smith/perl/ch-2.pl32
3 files changed, 70 insertions, 0 deletions
diff --git a/challenge-261/peter-campbell-smith/blog.txt b/challenge-261/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..292afaaa8c
--- /dev/null
+++ b/challenge-261/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/261
diff --git a/challenge-261/peter-campbell-smith/perl/ch-1.pl b/challenge-261/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..d7d2b79ee1
--- /dev/null
+++ b/challenge-261/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-03-18
+use utf8; # Week 261 - task 1 - Element digit sum
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+my (@ints, $j);
+
+element_digit_sum(1, 2, 3, 45);
+element_digit_sum(1, 12, 3);
+element_digit_sum(1, 2, 3, 4);
+element_digit_sum(236, 416, 336, 350);
+
+# longer random example
+for $j (0 .. 49) {
+ $ints[$j] = int(rand(1000));
+}
+element_digit_sum(@ints);
+
+sub element_digit_sum {
+
+ my (@ints, $digit_sum, $element_sum, $difference);
+
+ # calculate sums
+ @ints = @_;
+ $element_sum += $_ for @ints;
+ $digit_sum += $_ for split('', join('', @ints));
+
+ # calculate difference
+ $difference = abs($element_sum - $digit_sum);
+
+ say qq[\nInput: \@ints = (] . join(', ', @ints) . qq[)\n] .
+ qq[Output: $difference (element sum = $element_sum, digit sum = $digit_sum)];
+}
diff --git a/challenge-261/peter-campbell-smith/perl/ch-2.pl b/challenge-261/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..0995b67779
--- /dev/null
+++ b/challenge-261/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-03-18
+use utf8; # Week 261 - task 2 - Multiply by 2
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+multiply_by_2([5, 3, 6, 1, 12], 3);
+multiply_by_2([1, 2, 4, 3], 1);
+multiply_by_2([5, 6, 7], 2);
+multiply_by_2([64, 256, 4, 64, 128, 2, 16, 32, 8], 2);
+
+sub multiply_by_2 {
+
+ my (@ints, $start, $string);
+
+ # initialise
+ @ints = @{$_[0]};
+ $start = $_[1];
+
+ # join @ints as string
+ $string = ',' . join(',', @ints) . ',';
+
+ # search string as per task
+ $start *= 2 while ($string =~ m|,$start,|);
+
+ say qq[\nInput: \@ints = (] . join(', ', @ints) .
+ qq[}, \$start = $_[1]];
+ say qq[Output: $start];
+}