aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-07 17:22:44 +0100
committerGitHub <noreply@github.com>2024-05-07 17:22:44 +0100
commit978f0eda7234cfbf6e8ab7827f5afcb8893cc00b (patch)
treeaff680c79f3b0d32f7bf077fc1b0819b93cdcaa2
parentc1756b0e7aed0ad70fa63feb2565c69215c9d426 (diff)
parent5acde6e08dc96a9e79986ee5b7c396847b772e5d (diff)
downloadperlweeklychallenge-club-978f0eda7234cfbf6e8ab7827f5afcb8893cc00b.tar.gz
perlweeklychallenge-club-978f0eda7234cfbf6e8ab7827f5afcb8893cc00b.tar.bz2
perlweeklychallenge-club-978f0eda7234cfbf6e8ab7827f5afcb8893cc00b.zip
Merge pull request #10054 from choroba/ech268
Add solutions to 268: Magic Number & Number Game by E. Choroba
-rwxr-xr-xchallenge-268/e-choroba/perl/ch-1.pl17
-rwxr-xr-xchallenge-268/e-choroba/perl/ch-2.pl16
2 files changed, 33 insertions, 0 deletions
diff --git a/challenge-268/e-choroba/perl/ch-1.pl b/challenge-268/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..664bd89ee9
--- /dev/null
+++ b/challenge-268/e-choroba/perl/ch-1.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+use List::Util qw{ min };
+
+sub magic_number($x, $y) {
+ my @mins = map min(@$_), $x, $y;
+ return $mins[1] - $mins[0]
+}
+
+use Test::More tests => 3;
+
+is magic_number([3, 7, 5], [9, 5, 7]), 2, 'Example 1';
+is magic_number([1, 2, 1], [5, 4, 4]), 3, 'Example 2';
+is magic_number([2], [5]), 3, 'Example 3';
diff --git a/challenge-268/e-choroba/perl/ch-2.pl b/challenge-268/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..fff6aac962
--- /dev/null
+++ b/challenge-268/e-choroba/perl/ch-2.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub number_game(@ints) {
+ my @sorted = sort { $a <=> $b } @ints;
+ return [map { @sorted[$_ * 2 + 1, $_ * 2] } 0 .. $#sorted / 2]
+}
+
+use Test2::V0;
+plan(3);
+
+is number_game(2, 5, 3, 4), [3, 2, 5, 4], 'Example 1';
+is number_game(9, 4, 1, 3, 6, 4, 6, 1), [1, 1, 4, 3, 6, 4, 9, 6], 'Example 2';
+is number_game(1, 2, 2, 3), [2, 1, 3, 2], 'Example 3';