aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-06-04 10:58:46 +0100
committerGitHub <noreply@github.com>2024-06-04 10:58:46 +0100
commitf54dacce7d08ba5dd88b1f1694cfb66aee8615e9 (patch)
treeb39fb26ecec3ca94d86c14fdea6d07367f50bf76
parent14c73ef81e24547fee6ded800c16fb52f749b05d (diff)
parentf1cf90e9fc15df46ec9fa332826174697fd74854 (diff)
downloadperlweeklychallenge-club-f54dacce7d08ba5dd88b1f1694cfb66aee8615e9.tar.gz
perlweeklychallenge-club-f54dacce7d08ba5dd88b1f1694cfb66aee8615e9.tar.bz2
perlweeklychallenge-club-f54dacce7d08ba5dd88b1f1694cfb66aee8615e9.zip
Merge pull request #10199 from choroba/ech271
Add solutions to 271: Maximum Ones & Sort by 1 bits by E. Choroba
-rwxr-xr-xchallenge-271/e-choroba/perl/ch-1.pl28
-rwxr-xr-xchallenge-271/e-choroba/perl/ch-2.pl21
2 files changed, 49 insertions, 0 deletions
diff --git a/challenge-271/e-choroba/perl/ch-1.pl b/challenge-271/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..4e96f45bbc
--- /dev/null
+++ b/challenge-271/e-choroba/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+use PDL;
+
+sub maximum_ones($matrix) {
+ my $p = pdl($matrix);
+ my $sum = $p->sumover;
+ my $max = $sum->max;
+ return ($sum == $max)->which->unpdl->[0] + 1
+}
+
+use Test2::V0 qw{ plan is }; # Conflict with PDL::float.
+plan(3 + 1);
+
+is maximum_ones([[0, 1], [1, 0]]), 1, 'Example 1';
+is maximum_ones([[0, 0, 0], [1, 0, 1]]), 2, 'Example 2';
+is maximum_ones([[0, 0], [1, 1], [0, 0]]), 2, 'Example 3';
+
+
+is maximum_ones([[0, 0, 0, 0],
+ [1, 1, 0, 1],
+ [0, 0, 1, 0],
+ [1, 0, 0, 0],
+ [0, 1, 1, 1]]),
+ 2, 'Example 4';
diff --git a/challenge-271/e-choroba/perl/ch-2.pl b/challenge-271/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..561be4b901
--- /dev/null
+++ b/challenge-271/e-choroba/perl/ch-2.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub sort_by_1_bits(@ints) {
+ map $_->[1],
+ sort { $a->[0] <=> $b->[0]
+ ||
+ $a->[1] <=> $b->[1]
+ } map [unpack('%32b*', pack 'N', $_), $_],
+ @ints
+}
+
+use Test2::V0;
+plan(2);
+
+is [sort_by_1_bits(0, 1, 2, 3, 4, 5, 6, 7, 8)], [0, 1, 2, 4, 8, 3, 5, 6, 7],
+ 'Example 1';
+is [sort_by_1_bits(1024, 512, 256, 128, 64)], [64, 128, 256, 512, 1024],
+ 'Example 2';