aboutsummaryrefslogtreecommitdiff
path: root/challenge-343/barroff/raku/ch-1.p6
blob: eba555a481289b8b03c546dc22db70b9a020d7ab (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
#!/usr/bin/env raku

use v6.d;

sub zero-friend(@nums --> Int) {
    return 0 if 0@nums;
    my $above = min(grep({ $_ > 0 }, @nums));
    my $below = max(grep({ $_ < 0 }, @nums));
    return min($above, abs($below))
}

#| Run test cases
multi sub MAIN('test') {
    use Test;
    plan 5;

    is zero-friend([4, 2, -1, 3, -2]), 1, 'works for [4, 2, -1, 3, -2]';
    is zero-friend([-5, 5, -3, 3, -1, 1]), 1, 'works for [-5, 5, -3, 3, -1, 1]';
    is zero-friend([7, -3, 0, 2, -8]), 0, 'works for [7, -3, 0, 2, -8]';
    is zero-friend([-2, -5, -1, -8]), 1, 'works for [-2, -5, -1, -8]';
    is zero-friend([-2, 2, -4, 4, -1, 1]), 1, 'works for [-2, 2, -4, 4, -1, 1]';
}

#| Take user provided numbers 5 9 3 4 6
multi sub MAIN(*@nums) {
    say zero-friend(@nums);
}