aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-07-02 23:29:46 +0100
committerGitHub <noreply@github.com>2025-07-02 23:29:46 +0100
commit9b8a142d066824112f32998e348bf3cf72b543b8 (patch)
tree7f4e0bac2f89d29659ab5096076b6eba14ab5b4f
parentf16acd7ae04a3fe6ef70f747e8725d9d30acea75 (diff)
parentb46a009620f56c2aae6054b9e35a7a7228d28a6b (diff)
downloadperlweeklychallenge-club-9b8a142d066824112f32998e348bf3cf72b543b8.tar.gz
perlweeklychallenge-club-9b8a142d066824112f32998e348bf3cf72b543b8.tar.bz2
perlweeklychallenge-club-9b8a142d066824112f32998e348bf3cf72b543b8.zip
Merge pull request #12268 from pjcs00/wk328
Week 328 - Questions are good
-rw-r--r--challenge-328/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-328/peter-campbell-smith/perl/ch-1.pl40
-rwxr-xr-xchallenge-328/peter-campbell-smith/perl/ch-2.pl60
3 files changed, 101 insertions, 0 deletions
diff --git a/challenge-328/peter-campbell-smith/blog.txt b/challenge-328/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..006ff147dc
--- /dev/null
+++ b/challenge-328/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/328
diff --git a/challenge-328/peter-campbell-smith/perl/ch-1.pl b/challenge-328/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..6b8544a3e6
--- /dev/null
+++ b/challenge-328/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-06-30
+use utf8; # Week 328 - task 1 - Replace all ?
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+replace_all_queries('abc?def?ghi');
+replace_all_queries('?a?a?a?a?a?a?a?');
+replace_all_queries('a?????b');
+replace_all_queries('?????');
+replace_all_queries('?br?c?d?br?');
+replace_all_queries('a??????b');
+replace_all_queries('a?b?a?b?c?a');
+
+sub replace_all_queries {
+
+ my ($string, $z);
+
+ # initialise - add '*' to start and end
+ $string = qq[*$_[0]*];
+
+ # loop while string contains a '?'
+ while ($string =~ m|^(.*?)(.)\?(.?)(.*)|) {
+
+ # replace ? with a, b or c
+ for $z ('a', 'b', 'c') {
+ if ($z ne $2 and $z ne $3) {
+ $string = qq[$1$2$z$3$4];
+ last;
+ }
+ }
+ }
+
+ say qq[\nInput: $_[0]];
+ say qq[Output: ] . substr($string, 1, -1);
+}
diff --git a/challenge-328/peter-campbell-smith/perl/ch-2.pl b/challenge-328/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..dc94ae21e7
--- /dev/null
+++ b/challenge-328/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-06-30
+use utf8; # Week 328 - task 2 - Good string
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+good_string('WeEeekly');
+good_string('abcddeFfgghH');
+good_string('goodstringsGOODSTRINGS');
+good_string('BbaADdsSTtrRIinNGg');
+
+sub good_string {
+
+ my (@string, $p, @new_string, $last_p);
+
+ # initialise
+ @string = split('', $_[0]);
+ $p = 0;
+ $last_p = $#string;
+
+ # move partition along
+ while (1) {
+
+ # finished
+ last if $p >= $#string;
+
+ # pair at p, p + 1 is bad, cut them out and move partition left
+ if (is_bad($string[$p], $string[$p + 1])) {
+ @new_string = ();
+ @new_string = @string[0 .. $p - 1] if $p > 0;
+ @new_string = (@new_string, @string[$p + 2 .. $#string]) if $p + 2 <= $#string;
+ @string = @new_string;
+ $p -- if $p > 0;
+
+ # pair at p, p + 1 is good - move partition right
+ } else {
+ $p ++;
+ }
+ }
+
+ say qq[\nInput: '$_[0]'];
+ say qq[Output: '] . join('', @string) . q['];
+}
+
+sub is_bad {
+
+ # good unless they are the same character
+ return 0 unless lc($_[0]) eq lc($_[1]);
+
+ # bad if they are Xx or xX
+ return 1 if $_[0] le 'Z' and $_[1] ge 'a' or
+ $_[0] ge 'a' and $_[1] le 'Z';
+
+ # otherwise good (xx or XX)
+ return 0;
+}