aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-07-07 22:00:25 +0100
committerGitHub <noreply@github.com>2024-07-07 22:00:25 +0100
commitd79d16dfcc3c1f21e0699644f4cdb08401f4a2fe (patch)
tree93722f1c4851584750c552723d23ab8522820755
parent1324e20e76758ce2446c89c3dbdedc7f080266c9 (diff)
parent51a5c95f315b5f70610a1a744e6a18ae4f84c9ec (diff)
downloadperlweeklychallenge-club-d79d16dfcc3c1f21e0699644f4cdb08401f4a2fe.tar.gz
perlweeklychallenge-club-d79d16dfcc3c1f21e0699644f4cdb08401f4a2fe.tar.bz2
perlweeklychallenge-club-d79d16dfcc3c1f21e0699644f4cdb08401f4a2fe.zip
Merge pull request #10383 from kjetillll/challenge-271-kjetillll
https://theweeklychallenge.org/blog/perl-weekly-challenge-271/
-rw-r--r--challenge-271/kjetillll/perl/ch-1.pl29
-rw-r--r--challenge-271/kjetillll/perl/ch-2.pl27
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-271/kjetillll/perl/ch-1.pl b/challenge-271/kjetillll/perl/ch-1.pl
new file mode 100644
index 0000000000..56b1d2ac93
--- /dev/null
+++ b/challenge-271/kjetillll/perl/ch-1.pl
@@ -0,0 +1,29 @@
+use List::Util 'reduce'; use Test::More tests=>4; use strict; use warnings;
+
+sub maximum_ones {
+ my $row=0;
+ (
+ reduce {
+ $row++;
+ $b > $$a[0] ? [$b,$row] : $a
+ }
+ [-1,0],
+ map { 0 + grep $_==1, @$_ }
+ @{ pop() }
+ )
+ ->[ 1 ]
+}
+
+is maximum_ones( $$_{matrix} ), $$_{output}
+ for { matrix => [ [0, 1],
+ [1, 0] ], output => 1 },
+ { matrix => [ [0, 0, 0],
+ [1, 0, 1] ], output => 2 },
+ { matrix => [ [0, 0],
+ [1, 1],
+ [0, 0] ], output => 2 },
+ { matrix => [ [0, 0, 1],
+ [0, 1, 0],
+ [1, 1, 1],
+ [1, 1, 1],
+ [1, 0, 1] ], output => 3 };
diff --git a/challenge-271/kjetillll/perl/ch-2.pl b/challenge-271/kjetillll/perl/ch-2.pl
new file mode 100644
index 0000000000..6bbe5d427d
--- /dev/null
+++ b/challenge-271/kjetillll/perl/ch-2.pl
@@ -0,0 +1,27 @@
+use Test::More tests=>2; use strict; use warnings;
+
+sub one_bit_count {
+ sprintf("%b", pop) =~ y/1//
+}
+
+sub sort_by_one_bits {
+ map $$_[0],
+ sort { $$a[1] <=> $$b[1]
+ || $$a[0] <=> $$b[0] }
+ map [$_, one_bit_count($_) ],
+ @_
+}
+
+for my $test (
+ {
+ input => [0, 1, 2, 3, 4, 5, 6, 7, 8],
+ output => [0, 1, 2, 4, 8, 3, 5, 6, 7]
+ },
+ {
+ input => [1024, 512, 256, 128, 64],
+ output => [64, 128, 256, 512, 1024]
+ }
+) {
+ my @got = sort_by_one_bits( @{ $$test{input} } );
+ is_deeply( \@got, $$test{output}, "got: @got exp: @{$$test{output}}" );
+}