diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-05-26 16:55:15 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-05-26 17:03:12 +0100 |
| commit | 6bb0d6c5d3bc8f6adc2f3eaeee4cf60689e2d234 (patch) | |
| tree | 7895022f1b0e4692d140734d4466ea7e438b73b4 | |
| parent | 430f300fd7abb93023633e53f7d3d0768d0a8723 (diff) | |
| download | perlweeklychallenge-club-6bb0d6c5d3bc8f6adc2f3eaeee4cf60689e2d234.tar.gz perlweeklychallenge-club-6bb0d6c5d3bc8f6adc2f3eaeee4cf60689e2d234.tar.bz2 perlweeklychallenge-club-6bb0d6c5d3bc8f6adc2f3eaeee4cf60689e2d234.zip | |
Add Perl solution
| -rw-r--r-- | challenge-218/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-218/paulo-custodio/perl/ch-1.pl | 56 | ||||
| -rw-r--r-- | challenge-218/paulo-custodio/perl/ch-2.pl | 94 | ||||
| -rw-r--r-- | challenge-218/paulo-custodio/t/test-1.yaml | 20 | ||||
| -rw-r--r-- | challenge-218/paulo-custodio/t/test-2.yaml | 10 |
5 files changed, 182 insertions, 0 deletions
diff --git a/challenge-218/paulo-custodio/Makefile b/challenge-218/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-218/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-218/paulo-custodio/perl/ch-1.pl b/challenge-218/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..2172a1675a --- /dev/null +++ b/challenge-218/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl + +# Challenge 218 +# +# 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. +# Example 1 +# +# Input: @list = (3, 1, 2) +# Output: 6 +# +# 1 x 2 x 3 => 6 +# +# Example 2 +# +# Input: @list = (4, 1, 3, 2) +# Output: 24 +# +# 2 x 3 x 4 => 24 +# +# Example 3 +# +# Input: @list = (-1, 0, 1, 3, 1) +# Output: 3 +# +# 1 x 1 x 3 => 3 +# +# Example 4 +# +# Input: @list = (-8, 2, -9, 0, -4, 3) +# Output: 216 +# +# -9 × -8 × 3 => 216 + +use Modern::Perl; + +sub max_product { + my(@N) = @_; + return 0 if @N < 3; + my $max = 0; + for my $i (0 .. $#N-2) { + for my $j ($i+1 .. $#N-1) { + for my $k ($j+1 .. $#N) { + my $prod = $N[$i]*$N[$j]*$N[$k]; + $max = $prod if $prod > $max; + } + } + } + return $max; +} + +say max_product(@ARGV); diff --git a/challenge-218/paulo-custodio/perl/ch-2.pl b/challenge-218/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..15cdefc7b0 --- /dev/null +++ b/challenge-218/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,94 @@ +#!/usr/bin/perl + +# Challenge 218 +# +# 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. +# 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 +# +# Example 2: +# +# Input: @matrix = [ [0] ] +# Output: 1 + +use Modern::Perl; +use List::Util 'sum'; + +sub compute_score { + my(@game) = @_; + return sum(map {oct("0b$_")} @game); +} + +sub invert_row { + my($row, @game) = @_; + $game[$row] =~ tr/01/10/; + return @game; +} + +sub invert_col { + my($col, @game) = @_; + for (@game) { + $_ = substr($_,0,$col).(1-substr($_,$col,1)).substr($_,$col+1); + } + return @game; +} + +sub maximize_score { + my(@game) = @_; + my $max = compute_score(@game); + my $changed; + do { + $changed = 0; + for my $row (0..$#game) { + my @new_game = invert_row($row, @game); + my $new_max = compute_score(@new_game); + if ($new_max > $max) { + @game = @new_game; + $max = $new_max; + $changed = 1; + } + } + for my $col (0..length($game[0])-1) { + my @new_game = invert_col($col, @game); + my $new_max = compute_score(@new_game); + if ($new_max > $max) { + @game = @new_game; + $max = $new_max; + $changed = 1; + } + } + } while ($changed); + return @game; +} + +say compute_score(maximize_score(@ARGV)); diff --git a/challenge-218/paulo-custodio/t/test-1.yaml b/challenge-218/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..538bbd6a57 --- /dev/null +++ b/challenge-218/paulo-custodio/t/test-1.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 3 1 2 + input: + output: 6 +- setup: + cleanup: + args: 4 1 3 2 + input: + output: 24 +- setup: + cleanup: + args: -1 0 1 3 1 + input: + output: 3 +- setup: + cleanup: + args: -8 2 -9 0 -4 3 + input: + output: 216 diff --git a/challenge-218/paulo-custodio/t/test-2.yaml b/challenge-218/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..d22f9cb2fb --- /dev/null +++ b/challenge-218/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 0011 1010 1100 + input: + output: 39 +- setup: + cleanup: + args: 0 + input: + output: 1 |
