aboutsummaryrefslogtreecommitdiff
path: root/challenge-197/jo-37/perl/ch-1.pl
blob: 3a9bdb9c70e892cfee482ad1a94162b4fc5a9c8d (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
#!/usr/bin/perl -s

use v5.16;
use Test2::V0;

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...
    list of numbers

EOS


### Input and Output

say "(@{[move_zero(@ARGV)]})";


### Implementation

sub move_zero {
    my $cnt;
    # Count and suppress zeroes, then append the suppressed zeroes.
    ((grep {$cnt += !$_; $_} @_), (0) x $cnt)
}


### Examples and tests

sub run_tests {
    SKIP: {
        skip "examples" unless $examples;

        is [move_zero(1, 0, 3, 0, 0, 5)], [1, 3, 5, 0, 0, 0], 'example 1';
        is [move_zero(1, 6, 4)], [1, 6, 4], 'example 2';
        is [move_zero(0, 1, 0, 2, 0)], [1, 2, 0, 0, 0], 'example 3';
    }

    SKIP: {
        skip "tests" unless $tests;

        is [move_zero(0, 0, 0)], [0, 0, 0], 'zeroes only';
        is [move_zero(1, 0, -1)], [1, -1, 0], 'negative';
	}

    done_testing;
    exit;
}