aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-07-22 00:24:29 +0100
committerGitHub <noreply@github.com>2025-07-22 00:24:29 +0100
commit1560fd2263f3bc10789c68a77564b4f0acfa2cb0 (patch)
tree2b81ea6a81ad25c36900224f365dd0fd8e09911a
parent73724104536fa40079f4d32e9237ca2148f9c92b (diff)
parent5d0561cf98ed0896bc38d9545395c31b31a58522 (diff)
downloadperlweeklychallenge-club-1560fd2263f3bc10789c68a77564b4f0acfa2cb0.tar.gz
perlweeklychallenge-club-1560fd2263f3bc10789c68a77564b4f0acfa2cb0.tar.bz2
perlweeklychallenge-club-1560fd2263f3bc10789c68a77564b4f0acfa2cb0.zip
Merge pull request #12387 from kjetillll/challenge-331-kjetillll
https://theweeklychallenge.org/blog/perl-weekly-challenge-331/
-rw-r--r--challenge-331/kjetillll/perl/ch-1.pl5
-rw-r--r--challenge-331/kjetillll/perl/ch-2.pl36
2 files changed, 41 insertions, 0 deletions
diff --git a/challenge-331/kjetillll/perl/ch-1.pl b/challenge-331/kjetillll/perl/ch-1.pl
new file mode 100644
index 0000000000..d9e43bfe34
--- /dev/null
+++ b/challenge-331/kjetillll/perl/ch-1.pl
@@ -0,0 +1,5 @@
+sub f{ pop =~ /(\w+)\s*$/; length $1 }
+
+print f('The Weekly Challenge') == 9 ? "ok\n" : "err\n";
+print f(' Hello World ') == 5 ? "ok\n" : "err\n";
+print f("Let's begin the fun") == 3 ? "ok\n" : "err\n";
diff --git a/challenge-331/kjetillll/perl/ch-2.pl b/challenge-331/kjetillll/perl/ch-2.pl
new file mode 100644
index 0000000000..98d5cd9afc
--- /dev/null
+++ b/challenge-331/kjetillll/perl/ch-2.pl
@@ -0,0 +1,36 @@
+
+sub f {
+ my($a, $b) = @_;
+ $a =~ /^ #match from beginning of string $a
+ (.*) #substring before first swapped letter
+ (.) #first swapped letter
+ (.*) #substring between the two swapped letters
+ (.) #second swapped letter
+ (.*) #substring after the second swapped letter
+ $ #match up to end of string $a
+ (??{ #make the regexp engine try and retry different lengths of the three (.*) parts
+ #until the swapped string equals $b. Or it will give up with false as a result.
+ my $trial = "$1$4$3$2$5"; # $a with two letters swapped
+ $trial eq $b #check if the trial string now equals $b
+ ? '' #if so, no need to match anything more thus '', and the regex and also f() returns true
+ : '^' #make this trial fail with the impossible ^ match (beginning of string),
+ #and if no more trials the regex and also f() returns false
+ })
+ /x # /x ignores regexp whitespace and allows #comments
+}
+
+#sub f{my($a,$b)=@_;$a=~/^(.*)(.)(.*)(.)(.*)$(??{"$1$4$3$2$5"eq$b?'':'^'})/}
+
+print f("fudge", "fduge") ? "ok\n" : "err\n"; # true
+print f("love", "love") ? "err\n" : "ok\n"; # false
+print f("fodo", "food") ? "ok\n" : "err\n"; # true
+print f("feed", "feed") ? "ok\n" : "err\n"; # true
+
+print f("perl", "erlp") ? "err\n" : "ok\n"; # false
+print f("perl", "prel") ? "ok\n" : "err\n"; # true
+print f("regexp challenge", "regexp challenge") ? "ok\n" : "err\n"; # true, l and l swapped
+print f("regexp challenge", "regexg challenpe") ? "ok\n" : "err\n"; # true
+print f("challenge", "challegne") ? "ok\n" : "err\n"; # true
+print f("challenge", "challeneg") ? "ok\n" : "err\n"; # true
+print f("challenge", "hcallenge") ? "ok\n" : "err\n"; # true
+print f("challenge", "hcallegne") ? "err\n" : "ok\n"; # false, needs two swaps