aboutsummaryrefslogtreecommitdiff
path: root/challenge-234/jo-37/perl/ch-1.pl
blob: 7465c3500a35d5036e865b02e654c4ca2d9a133a (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
68
69
70
71
72
73
74
#!/usr/bin/perl -s

use v5.24;
use Test2::V0;
use List::Gather;
use warnings FATAL => 'all';
use experimental 'signatures';

our ($tests, $examples, $verbose);

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

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

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

STR...
    list of strings

EOS


### Input and Output

say qq{(@{[join ', ', map qq{"$_"}, common_chars(@ARGV)]})};


### Implementation

# Split the first string into its characters and try to remove the first
# occurrence of each character in every remaining string.  If the
# character was removed as many times as there are remaining words, it
# was part of all words.  Gather these.  Quoting the characters allows
# regex meta-characters within the given strings.

sub common_chars (@w) {
    gather for my $c (split //, shift @w) {
        take $c if @w == grep s/\Q$c//, @w;
    }
}


### Examples and tests

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

        is [common_chars("java", "javascript", "julia")],
            ["j", "a"], 'example 1';

        is [common_chars("bella", "label", "roller")],
            ["e", "l", "l"], 'example 2';

        is [common_chars("cool", "lock", "cook")],
            ["c", "o"], 'example 3';
    }

    SKIP: {
        skip "tests" unless $tests;

        is [common_chars(qw(abc bcd cde efg))], [], 'no common chars';
        is [common_chars("afedcbaf")], [qw(a f e d c b a f)], 'single word';
        is [common_chars(qw(a. [ab] a*))], ['a'], 'regex meta chars';
	}

    done_testing;
    exit;
}