From f5b63dee409f43f14c8d97e1e308bb13761567e8 Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 29 Sep 2025 13:05:46 +0100 Subject: Week 341 - Broken keys and mixed up words --- challenge-341/peter-campbell-smith/blog.txt | 1 + challenge-341/peter-campbell-smith/perl/ch-1.pl | 36 +++++++++++++++++++++++++ challenge-341/peter-campbell-smith/perl/ch-2.pl | 35 ++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 challenge-341/peter-campbell-smith/blog.txt create mode 100755 challenge-341/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-341/peter-campbell-smith/perl/ch-2.pl diff --git a/challenge-341/peter-campbell-smith/blog.txt b/challenge-341/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..412832778e --- /dev/null +++ b/challenge-341/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/341 diff --git a/challenge-341/peter-campbell-smith/perl/ch-1.pl b/challenge-341/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..ee198d597d --- /dev/null +++ b/challenge-341/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-09-29 +use utf8; # Week 341 - task 1 - Broken keyboard +use warnings; # Peter Campbell Smith + +binmode STDOUT, ':utf8'; +use Encode; + +broken_keyboard('Hello world', ['d']); +broken_keyboard('apple banana cherry', ['a', 'e']); +broken_keyboard('Coding is fun', []); +broken_keyboard('Perl and Python', ['p']); +broken_keyboard('aaaaa ccc b', ['a', 'b']); + +sub broken_keyboard { + + my ($string, $keys, $count); + + # initialise and concatenate broken keys + no warnings 'uninitialized'; + $string = $_[0]; + $keys .= $_ for @{$_[1]}; + + # delete words containing broken keys + $string =~ s|\w*[$keys]\w*||gi if $keys; + + # count the remaining words + $count ++ while $string =~ m|\w+|gi; + + # report + say qq[\nInput: \$string = '$_[0]', \@keys = ('] . join(q[', '], @{$_[1]}) . q[')]; + say qq[Output: ] . ($count + 0); +} diff --git a/challenge-341/peter-campbell-smith/perl/ch-2.pl b/challenge-341/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..aad0f9b74e --- /dev/null +++ b/challenge-341/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-09-29 +use utf8; # Week 341 - task 2 - Reverse prefix +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +reverse_prefix('programming', 'g'); +reverse_prefix('hello', 'h'); +reverse_prefix('goodbye', 'e'); +reverse_prefix('noitulos', 's'); +reverse_prefix('conundrum', 'z'); + +sub reverse_prefix { + + my ($string, $char, $result); + + # initialise + ($string, $char) = @_; + + # do as instructed + if ($string =~ m|(.*?)$char(.*)|) { + $result = qq['$char] . reverse($1) . qq[$2']; + + # non-compliant data! + } else { + $result = qq[There is no '$char' in '$string']; + } + + say qq[\nInput: \$string = '$string', \$char = '$char']; + say qq[Output: $result]; +} -- cgit