From de2e9521aa5bd74001134daafb71386035b31a62 Mon Sep 17 00:00:00 2001 From: Andreas Mahnke Date: Mon, 21 Jul 2025 14:15:29 +0200 Subject: Challenge 331 --- challenge-331/mahnkong/perl/ch-1.pl | 14 ++++++++++++++ challenge-331/mahnkong/perl/ch-2.pl | 29 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 challenge-331/mahnkong/perl/ch-1.pl create mode 100644 challenge-331/mahnkong/perl/ch-2.pl 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"); -- cgit