aboutsummaryrefslogtreecommitdiff
path: root/challenge-252/jo-37/perl/ch-2.pl
blob: 7269103def8c10062ad41f863292a1ec38f2b313 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/perl -s

use v5.26;
use Test2::V0;
use experimental 'signatures';

our ($tests, $examples);

run_tests() if $tests || $examples;	# does not return

die <<EOS unless @ARGV;
usage: $0 [-examples] [-tests] [N]

-examples
    run the examples from the challenge
 
-tests
    run some tests

N
    positive integer

EOS


### Input and Output

say "(@{[uniq_sum_zero(shift)]})";


### Implementation

sub uniq_sum_zero ($n) {
    (-$n / 2 .. -1, (0) x ($n % 2), 1 .. $n / 2);
}


### Examples and tests

sub run_tests {
    use List::Util qw(uniq sum);

    my sub test_usz {
        plan 2;
        is scalar(uniq @_), scalar @_, 'uniq';
        is sum(@_), 0, 'sum zero';
    }

    SKIP: {
        skip "examples" unless $examples;

        subtest 'example 1', \&test_usz, uniq_sum_zero(5);
        subtest 'example 2', \&test_usz, uniq_sum_zero(3);
        subtest 'example 3', \&test_usz, uniq_sum_zero(1);

    }

    SKIP: {
        skip "tests" unless $tests;

        subtest 'test 2', \&test_usz, uniq_sum_zero(2);
        subtest 'test 4', \&test_usz, uniq_sum_zero(4);
	}

    done_testing;
    exit;
}