diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-05-28 22:56:31 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-28 22:56:31 +0100 |
| commit | 1777abf746841ddb620c238c3b0680d39dc14b7d (patch) | |
| tree | e2a123e8b3873ed7148707b5e8e04cfcdae9d67b | |
| parent | 14cf8680ac73132109cb69b4ed593182f0d91612 (diff) | |
| parent | 4999b9d80ce0a6731837a89e407c467227832f62 (diff) | |
| download | perlweeklychallenge-club-1777abf746841ddb620c238c3b0680d39dc14b7d.tar.gz perlweeklychallenge-club-1777abf746841ddb620c238c3b0680d39dc14b7d.tar.bz2 perlweeklychallenge-club-1777abf746841ddb620c238c3b0680d39dc14b7d.zip | |
Merge pull request #8143 from Solathian/branch-for-challenge-218
Added file for challenge
| -rw-r--r-- | challenge-218/solathian/perl/ch-1.pl | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/challenge-218/solathian/perl/ch-1.pl b/challenge-218/solathian/perl/ch-1.pl new file mode 100644 index 0000000000..454a52a5ba --- /dev/null +++ b/challenge-218/solathian/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!usr/bin/perl +use v5.36; + +use Algorithm::Combinatorics qw(variations); +use List::Util qw(product); + +# Challenge 218 - 1 - Maximum Product + +# 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. + +maxProd(3, 1, 2); # 1 x 2 x 3 => 6 +maxProd(4, 1, 3, 2); # 2 x 3 x 4 => 24 +maxProd(-1, 0, 1, 3, 1); # 1 x 1 x 3 => 3 +maxProd(-8, 2, -9, 0, -4, 3); # -9 × -8 × 3 => 216 + +sub maxProd(@data) +{ + my @arrays = variations(\@data, 3); + + my $max; + my $maxArray; + + foreach my $arrayRef (@arrays) + { + my $prod = product(@$arrayRef); + + if((not defined $max) || ($prod > $max)) + { + $max = $prod; + $maxArray = $arrayRef; + } + + } + + say( join(" x ", @$maxArray), " => $max"); +}
\ No newline at end of file |
