From 335a38cca0d93b719811854ebbb825ae93c20517 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 21 Jul 2025 11:03:25 +0200 Subject: Add solutions to 331: Last Word & Buddy Strings by E. Choroba --- challenge-331/e-choroba/perl/ch-1.pl | 16 ++++++++++++++++ challenge-331/e-choroba/perl/ch-2.pl | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100755 challenge-331/e-choroba/perl/ch-1.pl create mode 100755 challenge-331/e-choroba/perl/ch-2.pl 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'; -- cgit