diff options
| -rw-r--r-- | challenge-271/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-271/dave-jacoby/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-271/dave-jacoby/perl/ch-2.pl | 39 |
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; +} + |
