aboutsummaryrefslogtreecommitdiff
path: root/challenge-220/athanasius/perl/ch-1.pl
blob: 81046f2b32a260038209883c236c4f0fe2e343da (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!perl

################################################################################
=comment

Perl Weekly Challenge 220
=========================

TASK #1
-------
*Common Characters*

Submitted by: Mohammad S Anwar

You are given a list of words.

Write a script to return the list of common characters (sorted alphabetically)
found in every word of the given list.

Example 1

  Input: @words = ("Perl", "Rust", "Raku")
  Output: ("r")

Example 2

  Input: @words = ("love", "live", "leave")
  Output: ("e", "l", "v")

=cut
################################################################################

#--------------------------------------#
# Copyright © 2023 PerlMonk Athanasius #
#--------------------------------------#

#===============================================================================
=comment

Assumption
----------
Non-alphabetic characters (e.g., the hyphen in "by-laws") should be ignored.

Interface
---------
If no command-line arguments are given, the test suite is run.

=cut
#===============================================================================

use strict;
use warnings;
use Const::Fast;
use Set::Tiny;
use Test::More;

const my $USAGE =>
"Usage:
  perl $0 [<words> ...]
  perl $0

    [<words> ...]    A list of words\n";

#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
    $| = 1;
    print "\nChallenge 220, Task #1: Common Characters (Perl)\n\n";
}

#===============================================================================
MAIN:
#===============================================================================
{
    if (scalar @ARGV == 0)
    {
        run_tests();
    }
    else
    {
        printf "Input:  \@words = (%s)\n", join ', ', map { qq["$_"] } @ARGV;

        my $common = find_common_characters( \@ARGV );

        printf "Output: (%s)\n",           join ', ', map { qq["$_"] } @$common;
    }
}

#-------------------------------------------------------------------------------
sub find_common_characters
#-------------------------------------------------------------------------------
{
    my ($words) = @_;
    my  $common = make_set( $words->[ 0 ] );

    for my $word ((@$words)[ 1 .. $#$words ])
    {
        $common = $common->intersection( make_set( $word ) );
    }

    return [ sort $common->elements ];
}

#-------------------------------------------------------------------------------
sub make_set
#-------------------------------------------------------------------------------
{
    my ($word) = @_;
    my  @chars = map { lc $_ } grep { / ^ [A-Za-z] $ /x } split //, $word;

    return Set::Tiny->new( @chars );
}

#-------------------------------------------------------------------------------
sub run_tests
#-------------------------------------------------------------------------------
{
    print "Running the test suite\n";

    while (my $line = <DATA>)
    {
        chomp $line;

        my  ($test_name, $words_str, $exptd_str) = split / \| /x, $line;

        for ($test_name, $words_str, $exptd_str)               # Trim whitespace
        {
            s/ ^ \s+   //x;
            s/   \s+ $ //x;
        }

        my @expected = split / \s+ /x,  $exptd_str;
        my @words    = split / \s+ /x,  $words_str;
        my $solution = find_common_characters( \@words );

        is_deeply $solution, \@expected, $test_name;
    }

    done_testing;
}

#-------------------------------------------------------------------------------
sub error
#-------------------------------------------------------------------------------
{
    my ($message) = @_;

    die "ERROR: $message\n$USAGE";
}

################################################################################

__DATA__
Example 1 |Perl    Rust          Raku      |r
Example 2 |love    live          leave     |e l v
Hyphenated|by-laws whale-blubber water-baby|a b w