aboutsummaryrefslogtreecommitdiff
path: root/challenge-331
diff options
context:
space:
mode:
authorAndreas Mahnke <andreas.mahnke@leuphana.de>2025-07-21 14:15:29 +0200
committerAndreas Mahnke <andreas.mahnke@leuphana.de>2025-07-21 14:15:29 +0200
commitde2e9521aa5bd74001134daafb71386035b31a62 (patch)
tree7e14f49e73ddb133891030a9fe5f67179023e98f /challenge-331
parente0f7e800fc8f1b2cb50896852a0a3ed61980f510 (diff)
downloadperlweeklychallenge-club-de2e9521aa5bd74001134daafb71386035b31a62.tar.gz
perlweeklychallenge-club-de2e9521aa5bd74001134daafb71386035b31a62.tar.bz2
perlweeklychallenge-club-de2e9521aa5bd74001134daafb71386035b31a62.zip
Challenge 331
Diffstat (limited to 'challenge-331')
-rw-r--r--challenge-331/mahnkong/perl/ch-1.pl14
-rw-r--r--challenge-331/mahnkong/perl/ch-2.pl29
2 files changed, 43 insertions, 0 deletions
diff --git a/challenge-331/mahnkong/perl/ch-1.pl b/challenge-331/mahnkong/perl/ch-1.pl
new file mode 100644
index 0000000000..9be87ad80e
--- /dev/null
+++ b/challenge-331/mahnkong/perl/ch-1.pl
@@ -0,0 +1,14 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run($str) {
+ return ($str =~ /([a-z]+)[^a-z]*$/i) ? length($1) : 0;
+}
+
+is(run('The Weekly Challenge'), 9, "Example 1");
+is(run(' Hello World '), 5, "Example 2");
+is(run("Let's begin the fun"), 3, "Example 3");
+is(run(" 3 "), 0, "Example 4");
+is(run(" "), 0, "Example 5");
diff --git a/challenge-331/mahnkong/perl/ch-2.pl b/challenge-331/mahnkong/perl/ch-2.pl
new file mode 100644
index 0000000000..b117c0bfa0
--- /dev/null
+++ b/challenge-331/mahnkong/perl/ch-2.pl
@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub check_buddy_string($ref, $compare_string, $i, $c_index) {
+ my @s = @{$ref};
+ my $c = $s[$i];
+ my $o = $s[$c_index];
+ $s[$i] = $o;
+ $s[$c_index] = $c;
+ return 1 if ($compare_string eq join('', @s));
+}
+
+sub run($str1, $str2) {
+ return 0 if (length($str1) != length($str2)) || length($str1) == 0;
+
+ my @str2 = split//, $str2;
+ for (my $i = 0; $i <= $#str2; $i++) {
+ return 1 if $i > 0 && check_buddy_string(\@str2, $str1, $i, $i-1);
+ return 1 if $i < $#str2 && check_buddy_string(\@str2, $str1, $i, $i+1);
+ }
+ return 0;
+}
+
+is(run("fuck", "fcuk"), 1, "Example 1");
+is(run("love", "love"), 0, "Example 2");
+is(run("fodo", "food"), 1, "Example 3");
+is(run("feed", "feed"), 1, "Example 4");