aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-07-02 23:25:45 +0100
committerGitHub <noreply@github.com>2025-07-02 23:25:45 +0100
commit80ba063fa0b7a24ec78c6be36552d5193810174f (patch)
tree43301cfaa617c4a7cfe9f94c87d2fbce5fadace3
parent744269604f2d12bf10431ff8451b05c74dd05f13 (diff)
parent46da78d7ea02d6ece1b8bfec1a3ccd17b6e69a03 (diff)
downloadperlweeklychallenge-club-80ba063fa0b7a24ec78c6be36552d5193810174f.tar.gz
perlweeklychallenge-club-80ba063fa0b7a24ec78c6be36552d5193810174f.tar.bz2
perlweeklychallenge-club-80ba063fa0b7a24ec78c6be36552d5193810174f.zip
Merge pull request #12263 from choroba/ech328
Add solutions to 328: Replace all ? & Good String by E. Choroba
-rwxr-xr-xchallenge-328/e-choroba/perl/ch-1.pl27
-rwxr-xr-xchallenge-328/e-choroba/perl/ch-2.pl17
2 files changed, 44 insertions, 0 deletions
diff --git a/challenge-328/e-choroba/perl/ch-1.pl b/challenge-328/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..c046a95abc
--- /dev/null
+++ b/challenge-328/e-choroba/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub replace_all_question_marks($str) {
+ my $pos = -1;
+ while (-1 != ($pos = index $str, '?', $pos)) {
+ my $char = 'a';
+ ++$char while $char eq substr($str, $pos - 1, 1)
+ || $char eq substr $str, $pos + 1, 1;
+ substr $str, $pos, 1, $char;
+ }
+ return $str
+}
+
+use Test::More tests => 3 + 5;
+
+is replace_all_question_marks('a?z'), 'abz', 'Example 1';
+is replace_all_question_marks('pe?k'), 'peak', 'Example 2';
+is replace_all_question_marks('gra?te'), 'grabte', 'Example 3';
+
+is replace_all_question_marks('a?b'), 'acb', 'Block two';
+is replace_all_question_marks('?a'), 'ba', 'Beginning of string';
+is replace_all_question_marks('a?'), 'ab', 'End of string';
+is replace_all_question_marks('a???b'), 'abacb', 'Chain reaction';
+is replace_all_question_marks('??b??'), 'acbab', 'Both ends';
diff --git a/challenge-328/e-choroba/perl/ch-2.pl b/challenge-328/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..749095d787
--- /dev/null
+++ b/challenge-328/e-choroba/perl/ch-2.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+my $bad = join '|', map "$_\u$_|\u$_$_", 'a' .. 'z';
+
+sub good_string($str) {
+ 1 while $str =~ s/$bad//;
+ return $str
+}
+
+use Test::More tests => 3;
+
+is good_string('WeEeekly'), 'Weekly', 'Example 1';
+is good_string('abBAdD'), '', 'Example 2';
+is good_string('abc'), 'abc', 'Example 3';