aboutsummaryrefslogtreecommitdiff
path: root/challenge-252/bruce-gray/raku/ch-2.raku
blob: 6a88fe477f1caedadcc20270c24de1736106f81d (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
sub task2 ( UInt $n ) {
    return ($n - $n²) div 2, |( 1 ..^ $n );
}
sub matches_task_specification ( UInt $n, @got --> Bool ) {
    # Array of _$n_ _unique_ _integers_ that _sum_ to _zero_.
    return False if @got.elems    !== $n;
    return False if @got.repeated !== 0;
    return False if @got.any      !~~ Int;
    return False if @got.sum      !== 0;
    return True;
}


# Many solutions to every $n except $n==1,
# so we cannot use the output from the task examples.
### my @tests =
###     # Two other possible solutions could be as below: (-5, -1, 1, 2, 3) and (-3, -1, 2, -2, 4).
###     ( 5, (-7, -1, 1, 3, 4) ),
###     ( 3, (-1, 0, 1) ),
###     ( 1, (0,) ),
### ;
use Test; plan 1;
constant $test_range = 1 .. 1_000;
my $fails = 0;
for $test_range -> $n {
    my @got = task2($n);
    if ! matches_task_specification($n, @got) {
        note "Failed: $n => @got[]";
        $fails++;
    }
}
is $fails, 0, "No failures when testing {$test_range.raku}";