diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-22 00:24:48 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-22 00:24:48 +0100 |
| commit | c7e5c648b6b0437f76dd7462bd75239ac8b70f84 (patch) | |
| tree | 22101ee4ce34e9c01393717844721db14422fb85 | |
| parent | 1560fd2263f3bc10789c68a77564b4f0acfa2cb0 (diff) | |
| parent | de2e9521aa5bd74001134daafb71386035b31a62 (diff) | |
| download | perlweeklychallenge-club-c7e5c648b6b0437f76dd7462bd75239ac8b70f84.tar.gz perlweeklychallenge-club-c7e5c648b6b0437f76dd7462bd75239ac8b70f84.tar.bz2 perlweeklychallenge-club-c7e5c648b6b0437f76dd7462bd75239ac8b70f84.zip | |
Merge pull request #12388 from mahnkong/challenge-331
Challenge 331
| -rw-r--r-- | challenge-331/mahnkong/perl/ch-1.pl | 14 | ||||
| -rw-r--r-- | challenge-331/mahnkong/perl/ch-2.pl | 29 |
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"); |
