# Perl Weekly Challenge: sums

These are some answers to the Week 51 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

I’m involved in the organization of local elections here in France this weekend and don’t have time for completing this week’s Perl Weekly Challenge. I’ll cover only task 1 (sums), and only in Raku.

Task 1: sums in Raku

Given an array @L of integers. Write a script to find all unique triplets such that a + b + c is same as the given target T. Also make sure a <= b <= c.

Here is wiki page for more information.

Example:

@L = (-25, -10, -7, -3, 2, 4, 8, 10);

One such triplet for target 0 i.e. -10 + 2 + 8 = 0.

We can simply use the built-in combinations method to generate all possible triplets and check fort each whether the sum is 0:

use v6;
constant TARGET = 0;

my @list = @*ARGS.elems > 0 ?? @*ARGS.sort !! (-25, -10, -7, -3, 2, 4, 8, 10);
for @list.combinations(3) -> $combination {
    say $combination if ([+] $combination) == TARGET;
}

Running this program with the default values produces the following output:

$ ./perl6 sums.p6
(-10 2 8)
(-7 -3 10)

It would be very easy to make the target value a parameter to the program, but I really don’t have time today.

Wrapping up

The next week Perl Weekly Challenge is due to start soon. If you want to participate in this challenge, please check https://perlweeklychallenge.org/ and make sure you answer the challenge before 23:59 BST (British summer time) on Sunday, March 22, 2020. And, please, also spread the word about the Perl Weekly Challenge if you can.