aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-218/barroff/gp/ch-1.gp30
-rw-r--r--challenge-218/barroff/perl/ch-1.pl31
-rw-r--r--challenge-218/barroff/raku/ch-1.raku29
-rw-r--r--challenge-218/barroff/vlang/ch-1.v20
-rw-r--r--challenge-218/barroff/vlang/ch-1_test.v9
5 files changed, 119 insertions, 0 deletions
diff --git a/challenge-218/barroff/gp/ch-1.gp b/challenge-218/barroff/gp/ch-1.gp
new file mode 100644
index 0000000000..d3cce5cf9d
--- /dev/null
+++ b/challenge-218/barroff/gp/ch-1.gp
@@ -0,0 +1,30 @@
+maximum_product(v, k = 3) =
+{
+ my(max_product = v[1] * v[2] * v[3], temp_prod);
+ forsubset([#v, k], s,
+ temp_prod = 1;
+ foreach(Vec(s), x, temp_prod *= v[x]);
+ if (temp_prod > max_product, max_product = temp_prod);
+ );
+ max_product;
+}
+
+
+l1 = [3, 1, 2];
+l2 = [4, 1, 3, 2];
+l3 = [-1, 0, 1, 3, 1];
+l4 = [-8, 2, -9, 0, -4, 3];
+
+mp = maximum_product(l1);
+if (mp == 6, print("Works for list one"), print("Did not work for list one"));
+
+mp = maximum_product(l2);
+if (mp == 24, print("Works for list two"), print("Did not work for list two"));
+
+mp = maximum_product(l3);
+if (mp == 3, print("Works for list three"), print("Did not work for list three"));
+
+mp = maximum_product(l4);
+if (mp == 216, print("Works for list four"), print("Did not work for list four"));
+
+quit();
diff --git a/challenge-218/barroff/perl/ch-1.pl b/challenge-218/barroff/perl/ch-1.pl
new file mode 100644
index 0000000000..cab1fe6a3f
--- /dev/null
+++ b/challenge-218/barroff/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+use v5.36;
+use strict;
+use warnings;
+
+use List::Util qw( max reduce );
+use Algorithm::Combinatorics qw( combinations );
+
+sub get_product (@numbers) {
+ return reduce { $a * $b } @numbers;
+}
+
+sub maximum_product ( $k, @s ) {
+ return max map( { get_product(@$_) } combinations( \@s, $k ) );
+}
+
+#| Run test cases
+sub MAIN() {
+ use Test2::V0 qw( is plan );
+ plan 4;
+
+ is maximum_product( 3, ( 3, 1, 2 ) ), 6, "works for (3, 1, 2)";
+ is maximum_product( 3, ( 4, 1, 3, 2 ) ), 24, "works for (4, 1, 3, 2)";
+ is maximum_product( 3, ( -1, 0, 1, 3, 1 ) ), 3,
+ "works for (-1, 0, 1, 3, 1)";
+ is maximum_product( 3, ( -8, 2, -9, 0, -4, 3 ) ), 216,
+ "works for (-8, 2, -9, 0, -4, 3)";
+}
+
+MAIN();
diff --git a/challenge-218/barroff/raku/ch-1.raku b/challenge-218/barroff/raku/ch-1.raku
new file mode 100644
index 0000000000..5f14669046
--- /dev/null
+++ b/challenge-218/barroff/raku/ch-1.raku
@@ -0,0 +1,29 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub maximum-product(Int:D @numbers where @numbers.elems ≥ 3, Int:D $k = 3 --> Int) {
+ return max(map({ [*] $_ }, @numbers.combinations($k)));
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 4;
+
+ is maximum-product(Array[Int].new(3, 1, 2)),
+ 6, "works for (3, 1, 2)";
+ is maximum-product(Array[Int].new(4, 1, 3, 2)),
+ 24, "works for (4, 1, 3, 2)";
+ is maximum-product(Array[Int].new(-1, 0, 1, 3, 1)),
+ 3, "works for (-1, 0, 1, 3, 1)";
+ is maximum-product(Array[Int].new(-8, 2, -9, 0, -4, 3)),
+ 216, "works for (-8, 2, -9, 0, -4, 3)";
+}
+
+#| Take user provided list like 1 2 3
+multi sub MAIN(*@numbers where @numbers.elems ≥ 3) {
+ my Int @int-numbers = @numbers;
+ say maximum-product(@int-numbers);
+}
+
diff --git a/challenge-218/barroff/vlang/ch-1.v b/challenge-218/barroff/vlang/ch-1.v
new file mode 100644
index 0000000000..0b98edefc4
--- /dev/null
+++ b/challenge-218/barroff/vlang/ch-1.v
@@ -0,0 +1,20 @@
+module main
+
+fn maximum_product(numbers []int) int {
+ mut max_prod := numbers[0] * numbers[1] * numbers[2]
+ mut temp_prod := 1
+ for x in 0 .. numbers.len - 2 {
+ for y in x + 1 .. numbers.len - 1 {
+ for z in y + 1 .. numbers.len {
+ temp_prod = numbers[x] * numbers[y] * numbers[z]
+ if temp_prod > max_prod {
+ max_prod = temp_prod
+ }
+ }
+ }
+ }
+ return max_prod
+}
+
+fn main() {
+}
diff --git a/challenge-218/barroff/vlang/ch-1_test.v b/challenge-218/barroff/vlang/ch-1_test.v
new file mode 100644
index 0000000000..ad61387008
--- /dev/null
+++ b/challenge-218/barroff/vlang/ch-1_test.v
@@ -0,0 +1,9 @@
+module main
+
+[assert_continues]
+fn test_lists() {
+ assert maximum_product([3, 1, 2]) == 6
+ assert maximum_product([4, 1, 3, 2]) == 24
+ assert maximum_product([-1, 0, 1, 3, 1]) == 3
+ assert maximum_product([-8, 2, -9, 0, -4, 3]) == 216
+}