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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
#!perl
################################################################################
=comment
Perl Weekly Challenge 344
=========================
TASK #2
-------
*Array Formation*
Submitted by: Mohammad Sajid Anwar
You are given two list: @source and @target.
Write a script to see if you can build the exact @target by putting these
smaller lists from @source together in some order. You cannot break apart or
change the order inside any of the smaller lists in @source.
Example 1
Input: @source = ([2,3], [1], [4])
@target = (1, 2, 3, 4)
Output: true
Use in the order: [1], [2,3], [4]
Example 2
Input: @source = ([1,3], [2,4])
@target = (1, 2, 3, 4)
Output: false
Example 3
Input: @source = ([9,1], [5,8], [2])
@target = (5, 8, 2, 9, 1)
Output: true
Use in the order: [5,8], [2], [9,1]
Example 4
Input: @source = ([1], [3])
@target = (1, 2, 3)
Output: false
Missing number: 2
Example 5
Input: @source = ([7,4,6])
@target = (7, 4, 6)
Output: true
Use in the order: [7, 4, 6]
=cut
################################################################################
#--------------------------------------#
# Copyright © 2025 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=comment
Assumptions
-----------
1. The elements of @source and @target may be any strings.
2. It must be possible to construct the target using elements from the source,
but there may be elements of @source which are not used in building @target.
Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. Two strings are entered on the command-line. The first (the source) comprises
a non-empty list of non-empty, square-bracket-delimited lists of strings,
e.g., "[2,3], [1], [4]". The second (the target) comprises a non-empty list
of strings, e.g., "1, 2, 3, 4". List elements are separated by commas and/or
whitespace.
=cut
#===============================================================================
use v5.38.2; # Enables strictures
use warnings;
use Const::Fast;
use Test::More;
const my $USAGE => <<END;
Usage:
perl $0 <source> <target>
perl $0
<source> Non-empty list of bracket-delimited lists of strings
<target> Non-empty list of strings
END
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 344, Task #2: Array Formation (Perl)\n\n";
}
#===============================================================================
MAIN:
#===============================================================================
{
my $argc = scalar @ARGV;
if ($argc == 0)
{
run_tests();
}
elsif ($argc == 2)
{
my ($source, $target) = parse_input( @ARGV );
printf "Input: \@source = (%s)\n",
join ', ', map { '[' . join( ',', @$_ ) . ']' } @$source;
printf " \@target = (%s)\n", join ', ', @$target;
my $formation = find_array_formation( $source, $target );
if (scalar @$formation == 0)
{
print "Output: false\n";
}
else
{
printf "Output: true\n\nUse in the order: %s\n",
join ', ', map { '[' . join( ',', @$_ ) . ']' } @$formation;
}
}
else
{
error( "Expected 0 or 2 command-line arguments, found $argc" );
}
}
#-------------------------------------------------------------------------------
sub find_array_formation
#-------------------------------------------------------------------------------
{
my ($source, $target) = @_;
my @source_strs = map { join '', @$_ } @$source;
my $target_str = join '', @$target;
my @formation = ();
my @indices = ();
if (recursive_search( \@source_strs, $target_str, \@indices ))
{
push @formation, $source->[$_] for @indices;
}
return \@formation;
}
#-------------------------------------------------------------------------------
sub recursive_search
#-------------------------------------------------------------------------------
{
my ($source, $target, $indices) = @_;
return 1 if $target eq '';
for my $i (0 .. $#$source)
{
my $src = $source->[$i];
next unless defined $src;
if ($target =~ / ^ $src .* /x)
{
my $new_target = substr $target, length $src;
my @new_source = @$source;
$new_source[$i] = undef;
if (recursive_search( \@new_source, $new_target, $indices ))
{
unshift @$indices, $i;
return 1;
}
}
}
return 0;
}
#-------------------------------------------------------------------------------
sub parse_input
#-------------------------------------------------------------------------------
{
my ($source_str, $target_str) = @_;
# Note: characters not contained within square brackets are silently ignored
my @source = map { [ split / [\s,]+ /x ] } $source_str =~ / \[ (.*?) \] /gx;
scalar @source > 0 or error( 'The source list is empty' );
for my $src (@source)
{
scalar @$src > 0 or error( 'Empty source list element' );
}
my @target = split / [\s,]+ /x, $target_str;
scalar @target > 0 or error( 'The target list is empty' );
return \@source, \@target;
}
#-------------------------------------------------------------------------------
sub run_tests
#-------------------------------------------------------------------------------
{
print "Running the test suite\n";
while (my $line = <DATA>)
{
chomp $line;
my ($test_name, $source_str, $target_str, $expected_str) =
split / \| /x, $line;
for ($test_name, $source_str, $target_str, $expected_str)
{
s/ ^ \s+ //x;
s/ \s+ $ //x;
}
my ($source, $target) = parse_input( $source_str, $target_str );
my $formation = find_array_formation( $source, $target );
my @expected = map { [ split /[\s,]+/ ] }
$expected_str =~ / \[ (.+?) \] /gx;
is_deeply $formation, \@expected, $test_name;
}
done_testing;
}
#-------------------------------------------------------------------------------
sub error
#-------------------------------------------------------------------------------
{
my ($message) = @_;
die "ERROR: $message\n$USAGE";
}
################################################################################
__DATA__
Example 1|[2 3] [1] [4] |1 2 3 4 |[1] [2 3] [4]
Example 2|[1 3] [2 4] |1 2 3 4 |
Example 3|[9 1] [5 8] [2] |5 8 2 9 1|[5 8] [2] [9 1]
Example 4|[1] [3] |1 2 3 |
Example 5|[7 4 6] |7 4 6 |[7 4 6]
Surplus |[2] [x y z] [9 1] [5 2] [5 8] [jk]|5 8 2 9 1|[5 8] [2] [9 1]
|