/
ch-1.pl
executable file
·63 lines (41 loc) · 1.23 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
#!/usr/bin/perl -s
use v5.24;
use Test2::V0;
our ($tests, $examples, $acronym);
run_tests() if $tests || $examples; # does not return
die <<EOS unless $acronym && @ARGV;
usage: $0 [-examples] [-tests] [-acronym=A WORD...]
-examples
run the examples from the challenge
-tests
run some tests
-acronym=A
check words against acronym A
WORD...
list of words
EOS
### Input and Output
say is_acronym($acronym, @ARGV) ? 'true' : 'false';
### Implementation
# Pick the first character of each word, convert these to lower case and
# compare the joined result to the lower case given acronym.
sub is_acronym {
# Need two "map"s here because "lc" forces scalar context and the
# regex match needs to be in list context.
lc(shift) eq join '', map lc, map /(.)/, @_;
}
### Examples and tests
sub run_tests {
SKIP: {
skip "examples" unless $examples;
ok is_acronym("ppp" => "Perl", "Python", "Pascal"), 'example 1';
ok !is_acronym("rp" => "Perl", "Raku"), 'example 2';
ok is_acronym("oac" => "Oracle", "Awk", "C"), 'example 3';
}
SKIP: {
skip "tests" unless $tests;
ok is_acronym("äöü" => "Ärger", "Öde", "Übel"), 'umlaute';
}
done_testing;
exit;
}