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
|
#!perl
################################################################################
=comment
Perl Weekly Challenge 208
=========================
TASK #1
-------
*Minimum Index Sum*
Submitted by: Mohammad S Anwar
You are given two arrays of strings.
Write a script to find out all common strings in the given two arrays with
minimum index sum. If no common strings found returns an empty list.
Example 1
Input: @list1 = ("Perl", "Raku", "Love")
@list2 = ("Raku", "Perl", "Hate")
Output: ("Perl", "Raku")
There are two common strings "Perl" and "Raku".
Index sum of "Perl": 0 + 1 = 1
Index sum of "Raku": 1 + 0 = 1
Example 2
Input: @list1 = ("A", "B", "C")
@list2 = ("D", "E", "F")
Output: ()
No common string found, so no result.
Example 3
Input: @list1 = ("A", "B", "C")
@list2 = ("C", "A", "B")
Output: ("A")
There are three common strings "A", "B" and "C".
Index sum of "A": 0 + 1 = 1
Index sum of "B": 1 + 2 = 3
Index sum of "C": 2 + 0 = 2
=cut
################################################################################
#--------------------------------------#
# Copyright © 2023 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=comment
Interface
---------
If no command-line arguments are given, the test suite is run.
=cut
#===============================================================================
use strict;
use warnings;
use Const::Fast;
use List::Util qw( min );
use Test::More;
const my $SEPARATOR => '-';
const my $USAGE =>
"Usage:
perl $0 [<list> ...]
perl $0
[<list> ...] <string list 1> - <string list 2>\n";
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 208, Task #1: Minimum Index Sum (Perl)\n\n";
}
#===============================================================================
MAIN:
#===============================================================================
{
my $args = scalar @ARGV;
if ($args == 0)
{
run_tests();
}
else
{
my ($list1, $list2) = parse_command_line( @ARGV );
printf "Input: \@list1 = (%s)\n", format_list( $list1 );
printf " \@list2 = (%s)\n", format_list( $list2 );
my $strings = find_min_idx_strings( $list1, $list2 );
printf "\nOutput: (%s)\n", format_list( $strings );
}
}
#-------------------------------------------------------------------------------
sub find_min_idx_strings
#-------------------------------------------------------------------------------
{
my ($list1, $list2) = @_;
my %common;
OUTER:
for my $i (0 .. $#$list1)
{
my $elem1 = $list1->[ $i ];
for my $j (0 .. $#$list2)
{
my $elem2 = $list2->[ $j ];
if ($elem1 eq $elem2)
{
push @{ $common{ $i + $j } }, $elem1;
next OUTER;
}
}
}
my $min_idx = min keys %common;
return defined $min_idx ? $common{ $min_idx } : [];
}
#-------------------------------------------------------------------------------
sub parse_command_line
#-------------------------------------------------------------------------------
{
my @args = @_;
my $idx = -1;
for my $i (0 .. $#args)
{
if ($args[ $i ] eq $SEPARATOR)
{
$idx = $i;
last;
}
}
$idx >= 0 or error( qq[Missing list separator "$SEPARATOR"] );
return ([ @args[ 0 .. $idx - 1 ] ],
[ @args[ $idx + 1 .. $#args ] ]);
}
#-------------------------------------------------------------------------------
sub format_list
#-------------------------------------------------------------------------------
{
my ($list) = @_;
return join ', ', map { qq["$_"] } @$list;
}
#-------------------------------------------------------------------------------
sub run_tests
#-------------------------------------------------------------------------------
{
print "Running the test suite\n";
while (my $line = <DATA>)
{
chomp $line;
my ($test_name, $input, $expected) = split / \| /x, $line;
$test_name =~ s/ \s+ $ //x;
my @args = split / \s+ /x, $input;
my ($list1, $list2) = parse_command_line( @args );
my $strings = find_min_idx_strings( $list1, $list2 );
my $got = join ' ', @$strings;
is $got, $expected, $test_name;
}
done_testing;
}
#-------------------------------------------------------------------------------
sub error
#-------------------------------------------------------------------------------
{
my ($message) = @_;
die "ERROR: $message\n$USAGE";
}
################################################################################
__DATA__
Example 1|Perl Raku Love - Raku Perl Hate|Perl Raku
Example 2|A B C - D E F |
Example 3|A B C - C A B |A
|