/
ch-2.raku
225 lines (172 loc) · 6.38 KB
/
ch-2.raku
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
use v6d;
################################################################################
=begin comment
Perl Weekly Challenge 264
=========================
TASK #2
-------
*Target Array*
Submitted by: Mohammad Sajid Anwar
You are given two arrays of integers, @source and @indices. The @indices can
only contains integers 0 <= i < size of @source.
Write a script to create target array by insert at index $indices[i] the value
$source[i].
Example 1
Input: @source = (0, 1, 2, 3, 4)
@indices = (0, 1, 2, 2, 1)
Output: (0, 4, 1, 3, 2)
@source @indices @target
0 0 (0)
1 1 (0, 1)
2 2 (0, 1, 2)
3 2 (0, 1, 3, 2)
4 1 (0, 4, 1, 3, 2)
Example 2
Input: @source = (1, 2, 3, 4, 0)
@indices = (0, 1, 2, 3, 0)
Output: (0, 1, 2, 3, 4)
@source @indices @target
1 0 (1)
2 1 (1, 2)
3 2 (1, 2, 3)
4 3 (1, 2, 3, 4)
0 0 (0, 1, 2, 3, 4)
Example 3
Input: @source = (1)
@indices = (0)
Output: (1)
=end comment
################################################################################
#--------------------------------------#
# Copyright © 2024 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=begin comment
Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. The lists $source and $indices are entered as named-argument strings on the
command-line.
- Each is a list of integers separated by whitespace.
- $indices contains unsigned integers (i, j, k, ...) such that each is less
than or equal to its own index position in the list: i = 0, 0 <= j <= 1,
0 <= k <= 2, etc.
=end comment
#===============================================================================
use Test;
subset MainArgs of List where (Array[Int], Array[UInt]);
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
"\nChallenge 264, Task #2: Target Array (Raku)\n".put;
}
#===============================================================================
multi sub MAIN
(
Str:D :$source, #= A whitespace-separated list of integers
Str:D :$indices #= A whitespace-separated list of positive integers
)
#===============================================================================
{
my MainArgs $args = parse-command-line( $source, $indices );
"Input: \@source = (%s)\n".printf: $args[ 0 ].join: ', ';
" \@indices = (%s)\n".printf: $args[ 1 ].join: ', ';
my Int @target = construct-target( $args );
"Output: \@target = (%s)\n".printf: @target.join: ', ';
}
#===============================================================================
multi sub MAIN() # No input: run the test suite
#===============================================================================
{
run-tests();
}
#-------------------------------------------------------------------------------
sub construct-target( MainArgs:D $args --> List:D[Int:D] )
#-------------------------------------------------------------------------------
{
my Int @target;
for 0 .. $args[ 0 ].end -> UInt $i
{
@target.splice( $args[ 1; $i ], 0, $args[ 0; $i ] );
}
return @target;
}
#-------------------------------------------------------------------------------
sub parse-command-line( Str:D $source, Str:D $indices --> MainArgs:D )
#-------------------------------------------------------------------------------
{
my Int @source;
for $source.split( / \s+ /, :skip-empty ) -> Str $s
{
+$s ~~ Int or error( qq["$s" is not a valid integer] );
@source.push: +$s;
}
my UInt @indices;
my UInt $count = 0;
for $indices.split( / \s+ /, :skip-empty ) -> Str $i
{
+$i ~~ Int or error( qq["$i" is not a valid integer] );
$i >= 0 or error( qq["$i" is negative] );
$i <= $count++ or error( qq[The index "$i" is too large] );
@indices.push: +$i;
}
@source.elems == @indices.elems
or error( 'The input lists are of different sizes' );
return @source, @indices;
}
#-------------------------------------------------------------------------------
sub run-tests()
#-------------------------------------------------------------------------------
{
'Running the test suite'.put;
for test-data.lines -> Str $line
{
my Str ($test-name, $source-str, $indices-str, $expected-str) =
$line.split: / \| /;
for $test-name, $source-str, $indices-str, $expected-str
{
s/ ^ \s+ //;
s/ \s+ $ //;
}
my Int @source = parse-list( $source-str );
my UInt @indices = parse-list( $indices-str );
my Int @expected = parse-list( $expected-str );
my Int @target = construct-target( [ @source, @indices ] );
is-deeply @target, @expected, $test-name;
}
done-testing;
}
#-------------------------------------------------------------------------------
sub parse-list( Str:D $string --> List:D[Int:D] )
#-------------------------------------------------------------------------------
{
return $string.split( / \s+ /, :skip-empty ).map( { .Int } ).list;
}
#-------------------------------------------------------------------------------
sub error( Str:D $message )
#-------------------------------------------------------------------------------
{
"ERROR: $message".put;
USAGE();
exit 0;
}
#-------------------------------------------------------------------------------
sub USAGE()
#-------------------------------------------------------------------------------
{
my Str $usage = $*USAGE;
$usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/;
$usage.put;
}
#-------------------------------------------------------------------------------
sub test-data( --> Str:D )
#-------------------------------------------------------------------------------
{
return q:to/END/;
Example 1|0 1 2 3 4|0 1 2 2 1|0 4 1 3 2
Example 2|1 2 3 4 0|0 1 2 3 0|0 1 2 3 4
Example 3|1 |0 |1
END
}
################################################################################