diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-22 00:19:48 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-22 00:19:48 +0100 |
| commit | 1af0dfa6d50b3821341341c418c3d6e7baf0a6db (patch) | |
| tree | 6552e79dafd1e7b33b16725cf51829d81d71bae8 | |
| parent | 0e28495f6326206c40a97822f6556953c0d89f04 (diff) | |
| parent | 335a38cca0d93b719811854ebbb825ae93c20517 (diff) | |
| download | perlweeklychallenge-club-1af0dfa6d50b3821341341c418c3d6e7baf0a6db.tar.gz perlweeklychallenge-club-1af0dfa6d50b3821341341c418c3d6e7baf0a6db.tar.bz2 perlweeklychallenge-club-1af0dfa6d50b3821341341c418c3d6e7baf0a6db.zip | |
Merge pull request #12382 from choroba/ech331
Add solutions to 331: Last Word & Buddy Strings by E. Choroba
| -rwxr-xr-x | challenge-331/e-choroba/perl/ch-1.pl | 16 | ||||
| -rwxr-xr-x | challenge-331/e-choroba/perl/ch-2.pl | 27 |
2 files changed, 43 insertions, 0 deletions
diff --git a/challenge-331/e-choroba/perl/ch-1.pl b/challenge-331/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..c7f884617c --- /dev/null +++ b/challenge-331/e-choroba/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub last_word($str) { + length +($str =~ /(\w+)/g)[-1] +} + +use Test::More tests => 3 + 1; + +is last_word('The Weekly Challenge'), 9, 'Example 1'; +is last_word(' Hello World '), 5, 'Example 2'; +is last_word("Let's begin the fun"), 3, 'Example 3'; + +is last_word("Let's"), 1, 'Word is a Perl word'; diff --git a/challenge-331/e-choroba/perl/ch-2.pl b/challenge-331/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..bea8ff680e --- /dev/null +++ b/challenge-331/e-choroba/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw( bitwise ); +use experimental qw( signatures ); + +sub buddy_strings($source, $target) { + my $xor = $source ^. $target; + return $xor =~ /^\x00*([^\x00])\1\x00*$/ ? 1 + : $source eq $target && $source =~ /(.)\1/ ? 1 + : 0 +} + +use Test2::V0; +use constant { + true => bool(1), + false => bool(0) +}; + +plan(4 + 1); + +is buddy_strings('fuck', 'fcuk'), true, 'Example 1'; +is buddy_strings('love', 'love'), false, 'Example 2'; +is buddy_strings('fodo', 'food'), true, 'Example 3'; +is buddy_strings('feed', 'feed'), true, 'Example 4'; + +is buddy_strings('nuclear', 'unclear'), true, 'Beginning'; |
