aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSolathian <horvath6@gmail.com>2023-05-28 14:22:03 +0200
committerSolathian <horvath6@gmail.com>2023-05-28 14:22:03 +0200
commit4999b9d80ce0a6731837a89e407c467227832f62 (patch)
tree2aa843857982768707a0d89ff6d3998c70a7e546
parentbcc11a8dc2fa51933b649f20cf0a3325b9193b6d (diff)
downloadperlweeklychallenge-club-4999b9d80ce0a6731837a89e407c467227832f62.tar.gz
perlweeklychallenge-club-4999b9d80ce0a6731837a89e407c467227832f62.tar.bz2
perlweeklychallenge-club-4999b9d80ce0a6731837a89e407c467227832f62.zip
Added file for challenge
-rw-r--r--challenge-218/solathian/perl/ch-1.pl37
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