diff options
| author | CY Fung <fungcheokyin@gmail.com> | 2023-05-29 01:15:08 +0800 |
|---|---|---|
| committer | CY Fung <fungcheokyin@gmail.com> | 2023-05-29 01:15:08 +0800 |
| commit | 266becae2fc5391bc78995e9e2ade1507b8e1cee (patch) | |
| tree | 5c6e07f55061e0f202ae235c9748d3fb48a7f463 | |
| parent | feb6e46d7181bae89707b9b9539b8bc7b51436bf (diff) | |
| download | perlweeklychallenge-club-266becae2fc5391bc78995e9e2ade1507b8e1cee.tar.gz perlweeklychallenge-club-266becae2fc5391bc78995e9e2ade1507b8e1cee.tar.bz2 perlweeklychallenge-club-266becae2fc5391bc78995e9e2ade1507b8e1cee.zip | |
Week 218
| -rw-r--r-- | challenge-218/cheok-yin-fung/perl/ch-1.pl | 25 | ||||
| -rw-r--r-- | challenge-218/cheok-yin-fung/perl/ch-2.pl | 36 |
2 files changed, 61 insertions, 0 deletions
diff --git a/challenge-218/cheok-yin-fung/perl/ch-1.pl b/challenge-218/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..54bcd892fd --- /dev/null +++ b/challenge-218/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,25 @@ +# The Weekly Challenge 218 +# Task 1 Maximum Product + +use v5.30.0; +use warnings; +use List::Util qw/max/; + +sub mp { + my @old_list = @_; + my @list = grep {$_ != 0} sort {$b<=>$a} @old_list; + return 0 if scalar @list < 3; + my $appp = $list[0]*$list[1]*$list[2]; + my $apnn = $list[0]*$list[-2]*$list[-1]; + # my $appn = $list[0]*$list[1]*$list[2]; + # my $annn = $list[0]*$list[1]*$list[2]; + return max($appp, $apnn); +} + +use Test::More tests=>5; +ok mp(3, 1, 2) == 6; +ok mp(4, 1, 3, 2) == 24; +ok mp(-1, 0, 1, 3, 1) == 3; +ok mp(-8, 2, -9, 0, -4, 3) == 216; + +ok mp(-4, -1, -3, -2) == -6; diff --git a/challenge-218/cheok-yin-fung/perl/ch-2.pl b/challenge-218/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..8713fda48c --- /dev/null +++ b/challenge-218/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,36 @@ +# The Weekly Challenge 218 +# Task 2 Matrix Score + +use v5.30.0; +use warnings; +use List::Util qw/max sum/; + +sub toggle_or_not { + return [@_] if $_[0] == 1; + # if $_[0] == 0 + my @ans = map {0+!$_} @_; + return [@ans]; +} + +sub ms { + my @old_rows = $_[0]->@*; + my @rows; + foreach (@old_rows) { + push @rows, toggle_or_not($_->@*); + } + my $score = 0; + my $height = scalar @rows; + my $len = $rows[0]->$#*; + for my $col (reverse 0..$len) { + my $ones = sum map {$rows[$_][$col]} 0..$#rows; + $score += 2**($len-$col) * max($height-$ones, $ones); + } + return $score; +} + + +use Test::More tests=>2; +ok ms( [ [0,0,1,1], + [1,0,1,0], + [1,1,0,0], ]) == 39; +ok ms([[0]]) == 1; |
