From e645e77e31e77730f0508f5b228789965066e404 Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 12 May 2025 23:25:35 +0100 Subject: Week 321 - Moving backwards --- challenge-321/peter-campbell-smith/blog.txt | 1 + challenge-321/peter-campbell-smith/perl/ch-1.pl | 35 ++++++++++++++++++++++++ challenge-321/peter-campbell-smith/perl/ch-2.pl | 36 +++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 challenge-321/peter-campbell-smith/blog.txt create mode 100755 challenge-321/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-321/peter-campbell-smith/perl/ch-2.pl 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']); +} -- cgit