diff options
| -rw-r--r-- | challenge-340/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-340/peter-campbell-smith/perl/ch-1.pl | 49 | ||||
| -rwxr-xr-x | challenge-340/peter-campbell-smith/perl/ch-2.pl | 41 |
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]; +} |
