From c3dc2d75c6a110082c7b3866cde4d50d088bd438 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 12 Feb 2024 07:39:58 +0000 Subject: w256 - Task 1 & 2 --- challenge-256/perlboy1967/perl/ch1.pl | 42 +++++++++++++++++++++++++++++++++++ challenge-256/perlboy1967/perl/ch2.pl | 38 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100755 challenge-256/perlboy1967/perl/ch1.pl create mode 100755 challenge-256/perlboy1967/perl/ch2.pl 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; -- cgit