diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-19 22:59:31 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-19 22:59:31 +0100 |
| commit | bbc894927eac3a4ea2963472231434211de6372e (patch) | |
| tree | 03c380c04eb9841d804d5cf6b354985609695053 | |
| parent | ca2f2ddc03aff6969a0c0f7002b4bf4f5be8b39d (diff) | |
| parent | 27eb075c10c937503175dfda539bda9f2d95c47f (diff) | |
| download | perlweeklychallenge-club-bbc894927eac3a4ea2963472231434211de6372e.tar.gz perlweeklychallenge-club-bbc894927eac3a4ea2963472231434211de6372e.tar.bz2 perlweeklychallenge-club-bbc894927eac3a4ea2963472231434211de6372e.zip | |
Merge pull request #10667 from pme/challenge-218
challenge-218
| -rwxr-xr-x | challenge-218/peter-meszaros/perl/ch-1.pl | 68 | ||||
| -rwxr-xr-x | challenge-218/peter-meszaros/perl/ch-2.pl | 89 |
2 files changed, 157 insertions, 0 deletions
diff --git a/challenge-218/peter-meszaros/perl/ch-1.pl b/challenge-218/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..e026d90d17 --- /dev/null +++ b/challenge-218/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,68 @@ +#!/usr/bin/env perl +# +=head1 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. + +=head2 Example 1 + + Input: @list = (3, 1, 2) + Output: 6 + + 1 x 2 x 3 => 6 + +=head2 Example 2 + + Input: @list = (4, 1, 3, 2) + Output: 24 + + 2 x 3 x 4 => 24 + +=head2 Example 3 + + Input: @list = (-1, 0, 1, 3, 1) + Output: 3 + + 1 x 1 x 3 => 3 + +=head2 Example 4 + + Input: @list = (-8, 2, -9, 0, -4, 3) + Output: 216 + + -9 x -8 x 3 => 216 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; +use List::Util qw/max/; + +my $cases = [ + [[ 3, 1, 2], 6, 'Example 1'], + [[ 4, 1, 3, 2], 24, 'Example 2'], + [[-1, 0, 1, 3, 1], 3, 'Example 3'], + [[-8, 2, -9, 0, -4, 3], 216, 'Example 4'], +]; + +sub maximum_product +{ + my $l = shift; + + my @sl = sort { $a <=> $b } @$l; + return max($sl[-1] * $sl[-2] * $sl[-3], $sl[0] * $sl[1] * $sl[-1]); +} + +for (@$cases) { + is(maximum_product($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-218/peter-meszaros/perl/ch-2.pl b/challenge-218/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..5c38da7f71 --- /dev/null +++ b/challenge-218/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,89 @@ +#!/usr/bin/env perl +# +=head1 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. + +=head2 Example 1: + + Input: @matrix = [ [0,0,1,1], + [1,0,1,0], + [1,1,0,0], ] + Output: 39 + + Move #1: convert row #1 => 1100 + [ [1,1,0,0], + [1,0,1,0], + [1,1,0,0], ] + + Move #2: convert col #3 => 101 + [ [1,1,1,0], + [1,0,0,0], + [1,1,1,0], ] + + Move #3: convert col #4 => 111 + [ [1,1,1,1], + [1,0,0,1], + [1,1,1,1], ] + + Score: 0b1111 + 0b1001 + 0b1111 => 15 + 9 + 15 => 39 + +=head2 Example 2: + + Input: @matrix = [ [0] ] + Output: 1 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[[0,0,1,1], + [1,0,1,0], + [1,1,0,0]], 39, 'Example 1'], + [[[0]], 1, 'Example 2'], +]; + +sub matrix_score +{ + my $m = shift; + + for my $r (@$m) { + $r = [map { 1 - $_ } $r->@*] if $r->[0] == 0; + } + for my $c (0 .. $#{$m->[0]}) { + my $ones = 0; + for my $r (@$m) { + $ones += $r->[$c]; + } + if ($ones < (@$m / 2)) { + for my $r (@$m) { + $r->[$c] = 1 - $r->[$c]; + } + } + } + + my $sum = 0; + for my $r (@$m) { + $sum += unpack("N", pack("B32", substr("0" x 32 . join('', $r->@*) , -32))); + } + return $sum; +} + +for (@$cases) { + is(matrix_score($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; |
