aboutsummaryrefslogtreecommitdiff
path: root/challenge-239/athanasius/perl/ch-1.pl
blob: 4eed2ac0eae4cc2009fbc06b2b1251ad36b797e4 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!perl

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

Perl Weekly Challenge 239
=========================

TASK #1
-------
*Same String*

Submitted by: Mohammad S Anwar

You are given two arrays of strings.

Write a script to find out if the word created by concatenating the array
elements is the same.

Example 1

  Input: @arr1 = ("ab", "c")
         @arr2 = ("a", "bc")
  Output: true

  Using @arr1, word1 => "ab" . "c" => "abc"
  Using @arr2, word2 => "a" . "bc" => "abc"

Example 2

  Input: @arr1 = ("ab", "c")
         @arr2 = ("ac", "b")
  Output: false

  Using @arr1, word1 => "ab" . "c" => "abc"
  Using @arr2, word2 => "ac" . "b" => "acb"

Example 3

  Input: @arr1 = ("ab", "cd", "e")
         @arr2 = ("abcde")
  Output: true

  Using @arr1, word1 => "ab" . "cd" . "e" => "abcde"
  Using @arr2, word2 => "abcde"

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

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

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

Assumptions
-----------
1. Concatenation may be performed ONLY in array order.
2. An empty array concatenates to the empty string.
3. The $SEPARATOR string is never used as an array element.

Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. If $VERBOSE is set to a true value (the default), the output ("True" or
   "False") is followed by details of the two concatenated strings.

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

use v5.32.1;
use warnings;
use Const::Fast;
use Test::More;

const my $VERBOSE   =>  1;
const my $SEPARATOR => '*';
const my $USAGE     =>
qq{Usage:
  perl $0 [<strings> ...]
  perl $0

    [<strings> ...]    Two lists of strings, separated by the token "*"\n};

#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
    $| = 1;
    print "\nChallenge 239, Task #1: Same String (Perl)\n\n";
}

#===============================================================================
MAIN:
#===============================================================================
{
    if (scalar @ARGV == 0)
    {
        run_tests();
    }
    else
    {
        my ($array1, $array2) = parse_input();

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

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

        my ($same, $word1, $word2) = same_string( $array1, $array2 );

        printf "Output: %s\n", $same ? 'True' : 'False';

        show_strings( $array1, $word1, $array2, $word2 ) if $VERBOSE;
    }
}

#-------------------------------------------------------------------------------
sub same_string
#-------------------------------------------------------------------------------
{
    my ($array1, $array2) = @_;
    my  $word1 = join '', @$array1;
    my  $word2 = join '', @$array2;

    return ($word1 eq $word2, $word1, $word2);
}

#-------------------------------------------------------------------------------
sub parse_input
#-------------------------------------------------------------------------------
{
    my @strings = @ARGV;
    my $count   = 0;

    for my $str (@strings)
    {
        ++$count if $str eq $SEPARATOR;
    }

    $count == 1 or error( 'The input list must contain exactly one separator' );

    my (@array1, @array2);
    my  $second = 0;

    for my $str (@strings)
    {
        if    ($second)
        {
            push @array2, $str;
        }
        elsif ($str eq $SEPARATOR)
        {
            $second = 1;
        }
        else
        {
            push @array1, $str;
        }
    }

    return (\@array1, \@array2);
}

#-------------------------------------------------------------------------------
sub show_strings
#-------------------------------------------------------------------------------
{
    my ($array1, $word1, $array2, $word2) = @_;

    print "\n";

    if (scalar @$array1 > 1)
    {
        printf qq[Using \@array1, word1 => %s => "%s"\n],
            join( ' . ', map { qq["$_"] } @$array1 ), $word1;
    }
    else
    {
        print  qq[Using \@array1, word1 => "$word1"\n];
    }

    if (scalar @$array2 > 1)
    {
        printf qq[Using \@array2, word2 => %s => "%s"\n],
            join( ' . ', map { qq["$_"] } @$array2 ), $word2;
    }
    else
    {
        print  qq[Using \@array2, word2 => "$word2"\n];
    }
}

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

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

        my  ($test, $arr1_str, $arr2_str, $exp_same, $exp_word1, $exp_word2) =
            split / \| /x, $line;

        for ($test, $arr1_str, $arr2_str, $exp_same, $exp_word1, $exp_word2)
        {
            s/ ^ \s+   //x;
            s/   \s+ $ //x;
        }

        my @array1 = split / \s+ /x, $arr1_str;
        my @array2 = split / \s+ /x, $arr2_str;

        my ($same, $word1, $word2) = same_string( \@array1, \@array2 );

        my $same_str = $same ? 'True' : 'False';

        is $same_str, $exp_same,  "$test: same";
        is $word1,    $exp_word1, "$test: word1";
        is $word2,    $exp_word2, "$test: word2";
    }

    done_testing;
}

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

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

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

__DATA__
Example 1|ab c   |a bc |True |abc  |abc
Example 2|ab c   |ac b |False|abc  |acb
Example 3|ab cd e|abcde|True |abcde|abcde