aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2024-02-12 07:39:58 +0000
committerNiels van Dijke <perlboy@cpan.org>2024-02-12 07:39:58 +0000
commitc3dc2d75c6a110082c7b3866cde4d50d088bd438 (patch)
tree93f0cf790bae2b000466f0e05d68b6a7c45fafdc
parent3f3e0798a68401ce1d67a5e1534f69de16856e82 (diff)
downloadperlweeklychallenge-club-c3dc2d75c6a110082c7b3866cde4d50d088bd438.tar.gz
perlweeklychallenge-club-c3dc2d75c6a110082c7b3866cde4d50d088bd438.tar.bz2
perlweeklychallenge-club-c3dc2d75c6a110082c7b3866cde4d50d088bd438.zip
w256 - Task 1 & 2
-rwxr-xr-xchallenge-256/perlboy1967/perl/ch1.pl42
-rwxr-xr-xchallenge-256/perlboy1967/perl/ch2.pl38
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-256/perlboy1967/perl/ch1.pl b/challenge-256/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..46d15ab60a
--- /dev/null
+++ b/challenge-256/perlboy1967/perl/ch1.pl
@@ -0,0 +1,42 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 256
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-256
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Maximum Pairs
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of distinct words, @words.
+
+Write a script to find the maximum pairs in the given array. The words $words[i]
+and $words[j] can be a pair one is reverse of the other.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+use common::sense;
+
+use Test2::V0;
+
+sub maximumPairs(@words) {
+ my ($n,%w) = (0);
+
+ for (@words) {
+ my $rev = reverse $_;
+ $n++ if exists $w{$rev};
+ $w{$_}++;
+ }
+
+ return $n;
+}
+
+is(maximumPairs(qw(ab de ed bc)),1);
+is(maximumPairs(qw(aa ba cd ed)),0);
+is(maximumPairs(qw(uv qp st vu mn pq)),2);
+
+done_testing;
diff --git a/challenge-256/perlboy1967/perl/ch2.pl b/challenge-256/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..0d47872c79
--- /dev/null
+++ b/challenge-256/perlboy1967/perl/ch2.pl
@@ -0,0 +1,38 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 256
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-256
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Merge Strings
+Submitted by: Mohammad Sajid Anwar
+
+You are given two strings, $str1 and $str2.
+
+Write a script to merge the given strings by adding in alternative order
+starting with the first string. If a string is longer than the other then
+append the remaining at the end.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+use common::sense;
+
+use Test2::V0;
+
+use List::MoreUtils qw(pairwise);
+
+sub mergeStrings ($s1,$s2) {
+ my @s1 = split //, $s1; my @s2 = split //, $s2;
+ join '', pairwise { ($a//'').($b//'') } @s1, @s2;
+}
+
+is(mergeStrings('abcd','1234'),'a1b2c3d4');
+is(mergeStrings('abc','12345'),'a1b2c345');
+is(mergeStrings('abcde','123'),'a1b2c3de');
+
+done_testing;