/
ch-2.pl
43 lines (34 loc) · 793 Bytes
/
ch-2.pl
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
43
#!/usr/bin/env perl
# The Weekly Challenge 244 - By Nelo Tovar
#
# https://theweeklychallenge.org/blog/perl-weekly-challenge-244/
#
# Task 2 - Group Hero
#
use strict;
use warnings;
use v5.28;
use List::Util qw (min max);
use Algorithm::Combinatorics qw(combinations);
use Data::Dump qw(dump);
my @examples = (
[ 2, 1, 4 ],
);
sub group_hero {
my $nums = shift;
my $length = scalar @$nums;
my $sum = 0;
for (my $k = 1; $k <= $length; $k++) {
my $iter = combinations($nums, $k);
while (my $c = $iter->next) {
$sum += (max(@$c) ** 2) * min(@$c)
}
}
return $sum;
}
for my $elements (@examples) {
my $gh = group_hero $elements;
say 'Input : @nums = ', dump(@$elements);
say 'Output : ', $gh;
say ' ';
}