aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-23 17:50:39 +0100
committerGitHub <noreply@github.com>2023-05-23 17:50:39 +0100
commit9ab359c82094e3d02fbc05a0da89f4e57f61c494 (patch)
treee600dcc61f5d8222a98ab2a1aa1d676a2f2e74dd
parentb3bac8208be82d9e2c3e3da8656cba70225c45c5 (diff)
parentb9798d4f8d8d14c5230c442253ad52e6994da391 (diff)
downloadperlweeklychallenge-club-9ab359c82094e3d02fbc05a0da89f4e57f61c494.tar.gz
perlweeklychallenge-club-9ab359c82094e3d02fbc05a0da89f4e57f61c494.tar.bz2
perlweeklychallenge-club-9ab359c82094e3d02fbc05a0da89f4e57f61c494.zip
Merge pull request #8126 from PerlBoy1967/branch-for-challenge-218
w218 - Task 1 & 2
-rwxr-xr-xchallenge-218/perlboy1967/perl/ch1.pl51
-rwxr-xr-xchallenge-218/perlboy1967/perl/ch2.pl58
2 files changed, 109 insertions, 0 deletions
diff --git a/challenge-218/perlboy1967/perl/ch1.pl b/challenge-218/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..48bbc2aa76
--- /dev/null
+++ b/challenge-218/perlboy1967/perl/ch1.pl
@@ -0,0 +1,51 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 217
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-217
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Maximum Product
+Submitted by: Mohammad S Anwar
+
+You are given a list of 3 or more integers.
+
+Write a script to find the 3 integers whose product is the maximum and return it.
+
+=cut
+
+use v5.16;
+
+use common::sense;
+
+use List::Util qw(product);
+
+use Test::More;
+
+sub maxProduct (@) {
+ my (@n,@p);
+
+ # Find negative and positive factors
+ for (@_) {
+ $_ < 0 ? push(@n,$_) :
+ $_ > 0 ? push(@p,$_) :
+ 0;
+ }
+
+ # If we have an odd number of negative factors,
+ # ditch the one closest to zero
+ @n = sort {$a <=> $b} @n;
+ pop @n if (@n % 2);
+
+ product(@p,@n);
+}
+
+is(maxProduct(3,2,1),6);
+is(maxProduct(4,1,3,2),24);
+is(maxProduct(-1,0,1,3,1),3);
+is(maxProduct(-8,2,-9,0,-4,3),432);
+is(maxProduct(-3,-2,-1,0,1,2,3),36);
+
+done_testing;
diff --git a/challenge-218/perlboy1967/perl/ch2.pl b/challenge-218/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..757630f319
--- /dev/null
+++ b/challenge-218/perlboy1967/perl/ch2.pl
@@ -0,0 +1,58 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 217
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-217
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Matrix Score
+Submitted by: Mohammad S Anwar
+
+You are given a m x n binary matrix i.e. having only 1 and 0.
+
+You are allowed to make as many moves as you want to get the highest score.
+
+|| A move can be either toggling each value in a row or column.
+
+To get the score, convert the each row binary to dec and return the sum.
+
+=cut
+
+use v5.16;
+
+use common::sense;
+
+use List::Util qw(sum);
+
+use Test::More;
+
+sub matrixScore (\@) {
+ my ($ar) = @_;
+ my $nR = @$ar - 1;
+ my $nC = @{$ar->[0]} - 1;
+
+ # First make sure each row has its MSB set
+ for (@$ar) {
+ map { $_ = ++$_ % 2 } @$_ if ($$_[0] == 0);
+ }
+
+ for my $c (1 .. $nC) {
+ my $ones = grep { $ar->[$_][$c] == 1 } (0 .. $nR);
+
+ # Flip bits in column C if number of '1's is smaller than # of rows
+ map { $ar->[$_][$c] = ++$$ar[$_][$c] % 2 } (0 .. $nR)
+ if ($ones < $nR);
+ }
+
+ return sum(map{oct('0b'.join('',@$_))} @$ar);
+}
+
+
+is(matrixScore(@{[[0,0,1,1],
+ [1,0,1,0],
+ [1,1,0,0],
+ ]}),39);
+
+done_testing;