aboutsummaryrefslogtreecommitdiff
path: root/challenge-268
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2024-05-06 10:42:52 +0000
committerNiels van Dijke <perlboy@cpan.org>2024-05-06 10:42:52 +0000
commit83093dc6a10963ea3f47c9e0cf36c7d2c73cb875 (patch)
tree4ebb7c03352dcb1e561d875ececfb2a9bf21fb34 /challenge-268
parent479ca4e9c71aa2ea5be2bc7b66451c21be9d8dde (diff)
downloadperlweeklychallenge-club-83093dc6a10963ea3f47c9e0cf36c7d2c73cb875.tar.gz
perlweeklychallenge-club-83093dc6a10963ea3f47c9e0cf36c7d2c73cb875.tar.bz2
perlweeklychallenge-club-83093dc6a10963ea3f47c9e0cf36c7d2c73cb875.zip
w267 & w268 - Task 1 & 2
Diffstat (limited to 'challenge-268')
-rwxr-xr-xchallenge-268/perlboy1967/perl/ch1.pl41
-rwxr-xr-xchallenge-268/perlboy1967/perl/ch2.pl45
2 files changed, 86 insertions, 0 deletions
diff --git a/challenge-268/perlboy1967/perl/ch1.pl b/challenge-268/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..74193f856b
--- /dev/null
+++ b/challenge-268/perlboy1967/perl/ch1.pl
@@ -0,0 +1,41 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 268
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-268
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Magic Number
+Submitted by: Mohammad Sajid Anwar
+
+You are given two arrays of integers of same size, @x and @y.
+
+Write a script to find the magic number that when added to each
+elements of one of the array gives the second array. Elements order
+is not important.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+use common::sense;
+
+use Test2::V0;
+
+use List::AllUtils qw(pairwise uniq);
+
+sub magicNumber($ar1,$ar2) {
+ my @d = uniq pairwise { $b - $a }
+ @{[sort {$a <=> $b} @$ar1]},
+ @{[sort {$a <=> $b} @$ar2]};
+ return (@d == 1 ? $d[0] : undef);
+}
+
+is(magicNumber([3,7,5],[9,5,7]),2,'Example 1');
+is(magicNumber([1,2,1],[5,4,4]),3,'Example 2');
+is(magicNumber([2],[5]),3,'Example 3');
+is(magicNumber([0,1],[2,4]),undef,'Own test');
+
+done_testing;
diff --git a/challenge-268/perlboy1967/perl/ch2.pl b/challenge-268/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..9e165a160e
--- /dev/null
+++ b/challenge-268/perlboy1967/perl/ch2.pl
@@ -0,0 +1,45 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 268
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-268
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Number Game
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of integers, @ints, with even number of elements.
+
+Write a script to create a new array made up of elements of the given
+array. Pick the two smallest integers and add it to new array in
+decreasing order i.e. high to low. Keep doing until the given array is
+empty.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+use common::sense;
+
+use Test2::V0;
+
+use List::AllUtils qw(all pairs pairwise);
+
+sub numberGame(@ints) {
+ my @before = @ints;
+ my @after;
+ while (1) {
+ push(@after,sort{$b<=>$a}@$_) for (pairs(sort{$a<=>$b}@before));
+ last if (all{$_ == 0} pairwise {$a-$b} @before,@after);
+ @before = @after; @after = ();
+ }
+ return @after;
+}
+
+is([numberGame(2,5,3,4)],[3,2,5,4],'Example 1');
+is([numberGame(9,4,1,3,6,4,6,1)],[1,1,4,3,6,4,9,6],'Example 2');
+is([numberGame(1,2,2,3)],[2,1,3,2],'Example 3');
+
+done_testing;