aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-328/wanderdoc/perl/ch-1.pl56
-rw-r--r--challenge-328/wanderdoc/perl/ch-2.pl61
2 files changed, 117 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;
+ }
+ }
+}
diff --git a/challenge-328/wanderdoc/perl/ch-2.pl b/challenge-328/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..a3371733b4
--- /dev/null
+++ b/challenge-328/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,61 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a string made up of lower and upper case English letters only.
+Write a script to return the good string of the given string. A string is called good string if it doesn't have two adjacent same characters, one in upper case and other is lower case.
+
+Example 1
+
+Input: $str = "WeEeekly"
+Output: "Weekly"
+
+We can remove either, "eE" or "Ee" to make it good.
+
+
+Example 2
+
+Input: $str = "abBAdD"
+Output: ""
+
+We remove "bB" first: "aAdD"
+Then we remove "aA": "dD"
+Finally remove "dD".
+
+
+Example 3
+
+Input: $str = "abc"
+Output: "abc"
+=cut
+
+use Test2::V0 -no_srand => 1;
+
+is(make_good_string("WeEeekly"), "Weekly", "Example 1");
+is(make_good_string("abBAdD"), "", "Example 2");
+is(make_good_string("abc"), "abc", "Example 3");
+done_testing();
+
+
+sub make_good_string
+{
+ my $str = $_[0];
+ my @arr = split(//, $str);
+ LOOP: while ( 1 )
+ {
+ return '' unless @arr;
+ for my $idx ( 0 .. $#arr - 1 )
+ {
+ if ( ord($arr[$idx]) - ord($arr[$idx+1]) == 32
+ or
+ ord($arr[$idx+1]) - ord($arr[$idx]) == 32
+ )
+ {
+ splice(@arr, $idx, 2);
+ next LOOP;
+ }
+ }
+ return join('', @arr);
+ }
+}