blob: 000a8b61409c0bf3ac36c32e6b1f72002a560377 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# Test: perl6 ch-1.p6
sub MAIN() {
say max-product([ 2, 5, -1, 3 ]);
}
sub max-product(@list) {
# Index of the array that will return
# the max product
my $left = 0;
my $right = @list.elems;
# Max product
my $max_product = 1;
# Loop through the list
for ^@list -> $i {
my $j = $i;
# Temp variable
my $current_product = 1;
# Loop through the list another time
while ($j < @list.elems) {
$current_product =
$current_product * @list[$j];
if ($current_product >= $max_product) {
$max_product = $current_product;
$left = $i;
$right = $j;
}
$j++;
}
}
return '[' ~
@list[$left .. $right].join(', ') ~
']' ~ ' which gives a maximum product of ' ~
$max_product;;
}
|