diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2023-12-30 17:39:02 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2023-12-30 17:39:02 +0000 |
| commit | c240b738b6c1e8d5aea6326f6f2d495a2d0216b5 (patch) | |
| tree | f071e421a0df653b819c763bf73be14d2d0dae00 | |
| parent | 4594fa62c682d8490a6caaea954c018df0a9014d (diff) | |
| download | perlweeklychallenge-club-c240b738b6c1e8d5aea6326f6f2d495a2d0216b5.tar.gz perlweeklychallenge-club-c240b738b6c1e8d5aea6326f6f2d495a2d0216b5.tar.bz2 perlweeklychallenge-club-c240b738b6c1e8d5aea6326f6f2d495a2d0216b5.zip | |
w249 - Task 1
| -rwxr-xr-x | challenge-249/perlboy1967/perl/ch1.pl | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-249/perlboy1967/perl/ch1.pl b/challenge-249/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..635383cc50 --- /dev/null +++ b/challenge-249/perlboy1967/perl/ch1.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 249 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-249 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Equal Pairs +Submitted by: Mohammad S Anwar + +You are given an array of integers with even number of elements. + +Write a script to divide the given array into equal pairs such that: + +a) Each element belongs to exactly one pair. +b) The elements present in a pair are equal. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); + +use Test2::V0; + +use List::MoreUtils qw(frequency); + +sub equalPairs (@ints) { + my %f = frequency @ints; + return [] if grep { $f{$_} % 2 } keys %f; + [map { [$_,$_] } map { ($_) x ($f{$_}>>1) } sort {$a <=> $b} keys %f]; +} + +is(equalPairs(3,2,3,2,2,2),[[2,2],[2,2],[3,3]]); +is(equalPairs(1,2,3,4,),[]); + +done_testing; |
