diff options
| author | wanderdoc <wanderdoc@users.noreply.github.com> | 2025-07-01 19:36:59 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-01 19:36:59 +0200 |
| commit | cc09cbf5b720ad6b03ebba85fe6e0e21dc901300 (patch) | |
| tree | 93acaf12a7ef9cf426b90adf32f944027f2aed74 | |
| parent | 7daf92e1dd4a2726fc578e87b6364ba4db3d5ad9 (diff) | |
| download | perlweeklychallenge-club-cc09cbf5b720ad6b03ebba85fe6e0e21dc901300.tar.gz perlweeklychallenge-club-cc09cbf5b720ad6b03ebba85fe6e0e21dc901300.tar.bz2 perlweeklychallenge-club-cc09cbf5b720ad6b03ebba85fe6e0e21dc901300.zip | |
Create ch-1.pl
| -rw-r--r-- | challenge-328/wanderdoc/perl/ch-1.pl | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/challenge-328/wanderdoc/perl/ch-1.pl b/challenge-328/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..e7c7c75609 --- /dev/null +++ b/challenge-328/wanderdoc/perl/ch-1.pl @@ -0,0 +1,56 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string containing only lower case English letters and ?. +Write a script to replace all ? in the given string so that the string doesn't contain consecutive repeating characters. + +Example 1 + +Input: $str = "a?z" +Output: "abz" + +There can be many strings, one of them is "abz". +The choices are 'a' to 'z' but we can't use either 'a' or 'z' to replace the '?'. + + +Example 2 + +Input: $str = "pe?k" +Output: "peak" + + +Example 3 + +Input: $str = "gra?te" +Output: "grabte" +=cut + +use Test2::V0 -no_srand => 1; + +is(replace_question_marks("a?z"), "abz", "Example 1"); +is(replace_question_marks("pe?k"), "peak", "Example 2"); +is(replace_question_marks("gra?te"), "grabte", "Example 3"); +is(replace_question_marks("gra?te?"), "grabtec", "Example 4"); +is(replace_question_marks("a??z"), "abcz", "Example 5"); +is(replace_question_marks("abc"), "abc", "Example 6"); +done_testing(); + +sub replace_question_marks +{ + my $str = $_[0]; + for my $ltr ( 'a' .. 'z' ) + { + my $copy = $str; + $copy =~ s/(\?)/$ltr/; + if ( $copy !~ /(?<char>.)\k<char>/) + { + $str = $copy; + } + if ($str !~ /\?/) + { + return $str; + } + } +} |
