aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-328/kjetillll/perl/ch-1.pl19
-rw-r--r--challenge-328/kjetillll/perl/ch-2.pl16
2 files changed, 35 insertions, 0 deletions
diff --git a/challenge-328/kjetillll/perl/ch-1.pl b/challenge-328/kjetillll/perl/ch-1.pl
new file mode 100644
index 0000000000..00e456535f
--- /dev/null
+++ b/challenge-328/kjetillll/perl/ch-1.pl
@@ -0,0 +1,19 @@
+
+sub f {
+ local $_ = shift;
+ #idea: using negative lookbehind and negative lookahead replace ? one at a time with a, b or c until no more ? is replaced:
+ 1 while
+ s/ (?<!a) \? (?!a) /a/x or #if ? neighter has a behind nor ahead, then replace it with a
+ s/ (?<!b) \? (?!b) /b/x or #if ? neighter has b behind nor ahead, then replace it with b
+ s/ (?<!c) \? (?!c) /c/x; #if ? neighter has c behind nor ahead, then replace it with c
+ $_
+}
+
+print f("a?z") eq "abz" ? "ok\n" : "err\n";
+print f("pe?k") eq "peak" ? "ok\n" : "err\n";
+print f("gra?te") eq "grabte" ? "ok\n" : "err\n";
+print f("a?????b") eq "ababacb" ? "ok\n" : "err\n";
+print f("?a?") eq "bab" ? "ok\n" : "err\n";
+print f("?b") eq "ab" ? "ok\n" : "err\n";
+print f("b???a?") eq "babcab" ? "ok\n" : "err\n";
+print f("?") eq "a" ? "ok\n" : "err\n";
diff --git a/challenge-328/kjetillll/perl/ch-2.pl b/challenge-328/kjetillll/perl/ch-2.pl
new file mode 100644
index 0000000000..fb88fd4616
--- /dev/null
+++ b/challenge-328/kjetillll/perl/ch-2.pl
@@ -0,0 +1,16 @@
+use v5.10;
+
+sub f {
+ state $regex_list = join '|', map { uc.lc, lc.uc } 'a' .. 'z'; #Aa|aA|Bb|bB ... Zz|zZ
+ my $str = shift;
+ 1 while $str =~ s/$regex_list//g;
+ $str
+}
+
+
+print f( "WeEeekly" ) eq "Weekly" ? "ok\n" : "err\n";
+print f( "abBAdD" ) eq "" ? "ok\n" : "err\n";
+print f( "abc" ) eq "abc" ? "ok\n" : "err\n";
+
+#'state' instead of 'my' inits $regex_list only once even though f() is called multiple times.
+#It also makes $regex_list invisible outside of sub f. Needs at least 'v5.10' for 'state' to be available.