diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-13 00:59:10 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-13 00:59:10 +0000 |
| commit | 4d05c81145f47c8f0f0da13b94eced07c9adf931 (patch) | |
| tree | bed1fed7b1a7dc380e33185436864e6ba2119a00 | |
| parent | 2c61f13242005b1d3ef4ca430ab1e94f0822ecf1 (diff) | |
| parent | ec58cdaab6e966972ce0256d9ffae8a6090df378 (diff) | |
| download | perlweeklychallenge-club-4d05c81145f47c8f0f0da13b94eced07c9adf931.tar.gz perlweeklychallenge-club-4d05c81145f47c8f0f0da13b94eced07c9adf931.tar.bz2 perlweeklychallenge-club-4d05c81145f47c8f0f0da13b94eced07c9adf931.zip | |
Merge pull request #9565 from PerlBoy1967/branch-for-challenge-256
w256 - Task 1 & 2
| -rwxr-xr-x | challenge-256/perlboy1967/perl/ch1.pl | 41 | ||||
| -rwxr-xr-x | challenge-256/perlboy1967/perl/ch2.pl | 38 |
2 files changed, 79 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..e08c285dd4 --- /dev/null +++ b/challenge-256/perlboy1967/perl/ch1.pl @@ -0,0 +1,41 @@ +#!/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) { + $n++ if exists $w{reverse $_}; + $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; |
