diff options
| author | E. Choroba <choroba@matfyz.cz> | 2025-06-30 11:34:56 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2025-06-30 11:34:56 +0200 |
| commit | 46da78d7ea02d6ece1b8bfec1a3ccd17b6e69a03 (patch) | |
| tree | cd1add1f0793b8322fbc3df8906797d00c469f8a | |
| parent | 7daf92e1dd4a2726fc578e87b6364ba4db3d5ad9 (diff) | |
| download | perlweeklychallenge-club-46da78d7ea02d6ece1b8bfec1a3ccd17b6e69a03.tar.gz perlweeklychallenge-club-46da78d7ea02d6ece1b8bfec1a3ccd17b6e69a03.tar.bz2 perlweeklychallenge-club-46da78d7ea02d6ece1b8bfec1a3ccd17b6e69a03.zip | |
Add solutions to 328: Replace all ? & Good String by E. Choroba
| -rwxr-xr-x | challenge-328/e-choroba/perl/ch-1.pl | 27 | ||||
| -rwxr-xr-x | challenge-328/e-choroba/perl/ch-2.pl | 17 |
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'; |
