aboutsummaryrefslogtreecommitdiff
path: root/challenge-335/athanasius/perl/ch-1.pl
blob: 46192cdbc88f78e051f4c1fc96e8e23181ff86f7 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!perl

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

Perl Weekly Challenge 335
=========================

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

Submitted by: Mohammad Sajid Anwar

You are given an array of words.

Write a script to return all characters that is in every word in the given array
including duplicates.

Example 1

  Input: @words = ("bella", "label", "roller")
  Output: ("e", "l", "l")

Example 2

  Input: @words = ("cool", "lock", "cook")
  Output: ("c", "o")

Example 3

  Input: @words = ("hello", "world", "pole")
  Output: ("l", "o")

Example 4

  Input: @words = ("abc", "def", "ghi")
  Output: ()

Example 5

  Input: @words = ("aab", "aac", "aaa")
  Output: ("a", "a")

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

#--------------------------------------#
# Copyright © 2025 PerlMonk Athanasius #
#--------------------------------------#

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

Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. A list of words is entered on the command-line.

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

use v5.32;       # Enables strictures
use warnings;
use Const::Fast;
use Set::Bag;
use Test::More;

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

    [<words> ...]    A list of words
END
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
    $| = 1;
    print "\nChallenge 335, Task #1: Common Characters (Perl)\n\n";
}

#===============================================================================
MAIN:
#===============================================================================
{
    if (scalar @ARGV == 0)
    {
        run_tests();
    }
    else
    {
        my @words = @ARGV;

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

        my $common = find_common_chars( \@words );

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

#-------------------------------------------------------------------------------
sub find_common_chars
#-------------------------------------------------------------------------------
{
    my ($words) = @_;
    my  $shared = Set::Bag->new;

    if (scalar @$words > 0)
    {
        $shared->insert( $_ => 1 ) for split //, $words->[ 0 ];

        for my $i (1 .. $#$words)
        {
            my $chars = Set::Bag->new;
               $chars->insert( $_ => 1 ) for split //, $words->[ $i ];

            $shared = $shared & $chars;             # Keep only the intersection
        }
    }

    my   @common;
    push @common, ($_) x $shared->grab( $_ ) for sort $shared->elements;

    return \@common;
}

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

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

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

        for ($test_name, $words_str, $exp_str)
        {
            s/ ^ \s+   //x;
            s/   \s+ $ //x;
        }

        my @words    = split / \s+ /x, $words_str;
        my $common   = find_common_chars( \@words );
        my @expected = split / \s+ /x, $exp_str;

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

    done_testing;
}

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

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

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

__DATA__
Example 1|bella label roller|e l l
Example 2|cool  lock  cook  |c o
Example 3|hello world pole  |l o
Example 4|abc   def   ghi   |
Example 5|aab   aac   aaa   |a a