aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-29 07:16:36 +0100
committerGitHub <noreply@github.com>2024-05-29 07:16:36 +0100
commit571bbb21267caa2fe9552af1d88be25a0a2888d7 (patch)
treeaaca02bdaf2aa1e448b76dc989eebb07214b7df5
parentdc2b802cd399d6ce85ca217a7830da188398818e (diff)
parent04f905d471708006f0c5886d45c172c57874975d (diff)
downloadperlweeklychallenge-club-571bbb21267caa2fe9552af1d88be25a0a2888d7.tar.gz
perlweeklychallenge-club-571bbb21267caa2fe9552af1d88be25a0a2888d7.tar.bz2
perlweeklychallenge-club-571bbb21267caa2fe9552af1d88be25a0a2888d7.zip
Merge pull request #10178 from jacoby/master
DAJ 271 blogged
-rw-r--r--challenge-271/dave-jacoby/blog.txt1
-rw-r--r--challenge-271/dave-jacoby/perl/ch-1.pl40
-rw-r--r--challenge-271/dave-jacoby/perl/ch-2.pl39
3 files changed, 80 insertions, 0 deletions
diff --git a/challenge-271/dave-jacoby/blog.txt b/challenge-271/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..4a408c867b
--- /dev/null
+++ b/challenge-271/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby-lpwk.onrender.com/2024/05/28/rows-without-a-paddle-weekly-challenge-271.html
diff --git a/challenge-271/dave-jacoby/perl/ch-1.pl b/challenge-271/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..7427d07108
--- /dev/null
+++ b/challenge-271/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ bitwise fc postderef say signatures state };
+
+use List::Util qw{max};
+
+my @examples = (
+
+ [ [ 0, 1 ], [ 1, 0 ], ],
+ [ [ 0, 0, 0 ], [ 1, 0, 1 ], ],
+ [ [ 0, 0 ], [ 1, 1 ], [ 0, 0 ], ],
+);
+for my $example (@examples) {
+ use JSON;
+ my $j = JSON->new->pretty->canonical;
+ my $output = maximum_ones($example);
+ my $input = display_matrix($example);
+ say <<"END";
+ Input: \$matrix =
+ [ $input ]
+ Output: $output
+END
+}
+
+sub maximum_ones ($matrix) {
+ my @rows = (0);
+ for my $r ( 1 .. scalar @$matrix ) {
+ $rows[$r] = scalar grep { $_ == 1 } $matrix->[ $r - 1 ]->@*;
+ }
+ my $max = max @rows;
+ my ($i) = grep { $rows[$_] == $max } 0 .. -1 + scalar @rows;
+ return $i;
+}
+
+sub display_matrix ($matrix) {
+ return join ",\n ",
+ map { join ' ', '[', ( join ', ', $_->@* ), ']' } $matrix->@*;
+}
diff --git a/challenge-271/dave-jacoby/perl/ch-2.pl b/challenge-271/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..2b9b658589
--- /dev/null
+++ b/challenge-271/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ fc say postderef signatures state };
+
+use List::Util qw{ sum0 };
+
+my @examples = (
+
+ [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ],
+ [ 1024, 512, 256, 128, 64 ],
+);
+
+for my $example (@examples) {
+ my @output = sort sb1 sort numeric $example->@*;
+ my $input = join ', ', $example->@*;
+ my $output = join ', ', @output;
+
+ say <<"END";
+ Input: \@ints = ($input)
+ Output: $output
+END
+}
+
+sub numeric {
+ return $a <=> $b ;
+}
+
+sub sb1 {
+ my $A = sprintf '%b', $a;
+ my $B = sprintf '%b', $b;
+ my $da = sum0 split //, $A;
+ my $db = sum0 split //, $B;
+ return -1 if $da < $db;
+ return 1 if $da > $db;
+ return 0;
+}
+