aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2025-05-12 23:25:35 +0100
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2025-05-12 23:25:35 +0100
commite645e77e31e77730f0508f5b228789965066e404 (patch)
treea08b2b5d35df88d31a0f4e5c03694b07c82d3875
parent62f1ccaddfc5a65501df9cfdf528d28927fef410 (diff)
downloadperlweeklychallenge-club-e645e77e31e77730f0508f5b228789965066e404.tar.gz
perlweeklychallenge-club-e645e77e31e77730f0508f5b228789965066e404.tar.bz2
perlweeklychallenge-club-e645e77e31e77730f0508f5b228789965066e404.zip
Week 321 - Moving backwards
-rw-r--r--challenge-321/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-321/peter-campbell-smith/perl/ch-1.pl35
-rwxr-xr-xchallenge-321/peter-campbell-smith/perl/ch-2.pl36
3 files changed, 72 insertions, 0 deletions
diff --git a/challenge-321/peter-campbell-smith/blog.txt b/challenge-321/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..e35e9de693
--- /dev/null
+++ b/challenge-321/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/321
diff --git a/challenge-321/peter-campbell-smith/perl/ch-1.pl b/challenge-321/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..5188d3ce9e
--- /dev/null
+++ b/challenge-321/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-05-12
+use utf8; # Week 321 - task 1 - Distinct average
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+distinct_average(1, 2, 4, 3, 5, 6);
+distinct_average(0, 2, 4, 8, 3, 5);
+distinct_average(7, 3, 1, 0, 5, 9);
+
+# larger example
+my @numbers;
+push @numbers, int(rand(100)) for 0 .. 99;
+distinct_average(@numbers);
+
+sub distinct_average {
+
+ my (@numbers, $j, %sums, $count);
+
+ @numbers = @_;
+ say qq[\nInput: (] . join(', ', @numbers) . q[)];
+
+ # sort numbers and sum them pairwise from each end
+ @numbers = sort {$a <=> $b} @numbers;
+ $sums{$numbers[$_] + $numbers[$#numbers - $_]} = 1 for 0 .. @numbers / 2;
+
+ # count the number of unique sums
+ $count ++ for keys %sums;
+
+ say qq[Output: $count];
+}
diff --git a/challenge-321/peter-campbell-smith/perl/ch-2.pl b/challenge-321/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..2941bb6faf
--- /dev/null
+++ b/challenge-321/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-05-12
+use utf8; # Week 321 - task 2 - Backspace compare
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+backspace_compare('ab#c', 'ad#c');
+backspace_compare('ab##', 'a#b#');
+backspace_compare('same', 'same');
+backspace_compare('#same', '#same');
+backspace_compare('a#b', 'c');
+backspace_compare('the curfewww## tolls the knelllll### of paaaaa####rting day',
+ 'z#the curfew tollxx##s the knell ofzzz### parting dayw#');
+
+sub backspace_compare {
+
+ my ($string1, $string2, $z, $s);
+
+ # initialise
+ ($string1, $string2) = @_;
+ say qq[\nInput: \$string1 = '$string1',\n \$string2 = '$string2'];
+
+ # repeatedly delete backspace and preceding character
+ for $s ($string1, $string2) {
+ $z = 1;
+ $z = ($s =~ s|.#||) while $z;
+ }
+
+ # report
+ say qq[Output: ] . ($string1 eq $string2 ? qq[true - '$string1'] :
+ qq[false - '$string1' vs '$string2']);
+}