-
Notifications
You must be signed in to change notification settings - Fork 311
/
ch-1.pl
executable file
·65 lines (41 loc) · 1.16 KB
/
ch-1.pl
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
#!/usr/bin/perl -s
use v5.24;
use Test2::V0;
use List::Util 'reduce';
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 product_sign(@ARGV);
### Implementation
#
# For details see:
# https://github.sommrey.de/the-bears-den/2024/05/03/ch-267.html#task-1
sub product_sign {
eval {reduce {($b || die) < 0 ? -$a : $a} 1, @_} // 0;
}
### Examples and tests
sub run_tests {
SKIP: {
skip "examples" unless $examples;
is product_sign(-1, -2, -3, -4, 3, 2, 1), 1, 'example 1';
is product_sign(1, 2, 0, -2, -1), 0, 'example 2';
is product_sign(-1, -1, 1, -1, 2), -1, 'example 3';
}
SKIP: {
skip "tests" unless $tests;
is product_sign( 10, (-10) x (1e6 - 1)), -1, 'large negative product';
is product_sign( 0, (-10) x (1e6 - 1)), 0, 'zero product';
is product_sign(-10, (-10) x (1e6 - 1)), 1, 'large positive product';
}
done_testing;
exit;
}