aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2025-09-22 16:50:51 +0100
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2025-09-22 16:50:51 +0100
commitf96db0fa4edf16e5f549078bab734299c70a2cd0 (patch)
tree8367be58d9167953a2121cca8691442483061537
parentc4e70544812c339e0344ad3127de18a5dbf98c34 (diff)
downloadperlweeklychallenge-club-f96db0fa4edf16e5f549078bab734299c70a2cd0.tar.gz
perlweeklychallenge-club-f96db0fa4edf16e5f549078bab734299c70a2cd0.tar.bz2
perlweeklychallenge-club-f96db0fa4edf16e5f549078bab734299c70a2cd0.zip
Week 340 - Deduplicate and going up
-rw-r--r--challenge-340/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-340/peter-campbell-smith/perl/ch-1.pl49
-rwxr-xr-xchallenge-340/peter-campbell-smith/perl/ch-2.pl41
3 files changed, 91 insertions, 0 deletions
diff --git a/challenge-340/peter-campbell-smith/blog.txt b/challenge-340/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..2ada4532ac
--- /dev/null
+++ b/challenge-340/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/340
diff --git a/challenge-340/peter-campbell-smith/perl/ch-1.pl b/challenge-340/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..fd8b809f13
--- /dev/null
+++ b/challenge-340/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-09-22
+use utf8; # Week 340 - task 1 - Duplicate removals
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+duplicate_removals('abbaca');
+duplicate_removals('aaaaaaaa');
+duplicate_removals('abcdabcd');
+duplicate_removals('abcdeedcbaf');
+duplicate_removals('oodrrunggnpeellyyligoogcccazztfghhgfe');
+duplicate_removals('xabcdeedcbay');
+
+sub duplicate_removals {
+
+ my ($string);
+ say qq[\nInput: '$_[0]'];
+
+ # imethod 1 - concise
+ $string = $_[0];
+ $string =~ s|$1$1|| while $string =~ m|([a-z])\1|;
+ say qq[Output1: '$string'];
+
+ # method 2 - faster?
+ my ($last, $output, $j, $char);
+
+ $string = $_[0];
+ $last = '#';
+ $output = '';
+ for $j (0 .. length($string) - 1) {
+ $char = substr($string, $j, 1);
+
+ # this char same as last - discard both
+ if ($char eq $last) {
+ $output = substr($output, 0, -1);
+ $last = substr($output, -1);
+
+ # this char differs from last - append it
+ } else {
+ $output .= $char;
+ $last = $char;
+ }
+ }
+ say qq[Output2: '$output'];
+}
diff --git a/challenge-340/peter-campbell-smith/perl/ch-2.pl b/challenge-340/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..0826a171c0
--- /dev/null
+++ b/challenge-340/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-09-22
+use utf8; # Week 340 - task 2 - Ascending numbers
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+ascending_numbers('the 1 cat sat on the 2 mats');
+ascending_numbers('the 2 cats sat on the 1 mat');
+ascending_numbers('the 3 cats sat on the 3 mats');
+ascending_numbers('one 1 two 2 three 3 four 4 five six 5 6 oops 5');
+ascending_numbers('0');
+ascending_numbers('cat');
+
+sub ascending_numbers {
+
+ my ($string, $last);
+
+ # initialise
+ $string = $_[0];
+ say qq[\nInput: '$string'];
+ $last = -1;
+
+ # loop over numbers
+ while ($string =~ m|(\d+)|g) {
+
+ # ok
+ if ($1 > $last) {
+ $last = $1;
+ next;
+ }
+
+ # not ok
+ say qq[Output: false ($1)];
+ return;
+ }
+ say qq[Output: true];
+}