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
|
use v6d;
###############################################################################
=begin comment
Perl Weekly Challenge 200
=========================
TASK #1
-------
*Arithmetic Slices*
Submitted by: Mohammad S Anwar
You are given an array of integers.
Write a script to find out all Arithmetic Slices for the given array of
integers.
An integer array is called arithmetic if it has at least 3 elements and the
differences between any three consecutive elements are the same.
Example 1
Input: @array = (1,2,3,4)
Output: (1,2,3), (2,3,4), (1,2,3,4)
Example 2
Input: @array = (2)
Output: () as no slice found.
=end comment
###############################################################################
#--------------------------------------#
# Copyright © 2023 PerlMonk Athanasius #
#--------------------------------------#
#==============================================================================
=begin comment
Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. If the first argument is negative, it must be preceded by "--" to distin-
guish it from a command-line flag.
Output Order
------------
As per Example 1, the slices are ordered by increasing size; slices of the same
size are preserved in the order in which they were found (scanning the array by
increasing indices). Note that the simple statement:
@slices.= sort: { $^a.elems <=> $^b.elems };
is sufficient here because sorting in Raku is stable [1].
Note
----
In the case where the difference between consecutive elements is zero, the same
slice may appear multiple times in the output. For example, an input array of
(4,4,4,4) produces the output (4,4,4), (4,4,4), (4,4,4,4). I assume this is the
desired result.
Reference
---------
[1] https://docs.raku.org/syntax/stable%20sort#:~:text=Computer%20scientists%20
call%20this%20a,the%20number%20of%20matches%20won
=end comment
#==============================================================================
use Test;
my UInt constant $TEST-FIELDS = 3;
#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
"\nChallenge 200, Task #1: Arithmetic Slices (Raku)\n".put;
}
#==============================================================================
multi sub MAIN
(
#| A list of 1 or more integers
*@array where { .elems >= 1 && .all ~~ Int:D }
)
#==============================================================================
{
"Input: \@array = (%s)\n".printf: @array.join: ',';
my Array[Int] @slices = find-arithmetic-slices( @array );
"Output: %s\n".printf: format-slices( @slices );
}
#==============================================================================
multi sub MAIN() # No input: run the test suite
#==============================================================================
{
run-tests();
}
#------------------------------------------------------------------------------
sub find-arithmetic-slices( List:D[Int:D] $array --> List:D[List:D[Int:D]] )
#------------------------------------------------------------------------------
{
my Array[Int] @slices;
for 0 .. $array.end - 2 -> UInt $i
{
my Int $gap = $array[ $i + 1 ] - $array[ $i ];
L-INNER:
for $i + 2 .. $array.end -> UInt $j
{
if $array[ $j ] - $array[ $j - 1 ] == $gap
{
@slices.push: Array[Int].new: $array[ $i .. $j ];
}
else
{
last L-INNER;
}
}
}
@slices.= sort: { $^a.elems <=> $^b.elems }; # Raku sorting is stable [1]
return @slices;
}
#------------------------------------------------------------------------------
sub format-slices( List:D[List:D[Int:D]] $slices --> Str:D )
#------------------------------------------------------------------------------
{
my Str $gist = '()';
if $slices.elems > 0
{
$gist = $slices.map( { '(' ~ @$_.join( ',' ) ~ ')' } ).join: ', ';
}
return $gist;
}
#------------------------------------------------------------------------------
sub run-tests()
#------------------------------------------------------------------------------
{
'Running the test suite'.put;
for test-data.lines -> Str $line
{
my Str ($test-name, $input, $expected) =
$line.split: / \| /, $TEST-FIELDS;
$input ~~ s/ ^ \s* (.+?) \s* $ /$0/; # Trim whitespace
$expected ~~ s/ ^ \s* (.+?) \s* $ /$0/;
$expected ~~ s:g/ \s+ / /;
my Int @array = $input.split( / \, \s* / ).map: { .Int };
my Array[Int] @slices = find-arithmetic-slices( @array );
my Str $got = format-slices( @slices );
is $got, $expected, $test-name;
}
done-testing;
}
#------------------------------------------------------------------------------
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 | 1,2,3,4 |(1,2,3), (2,3,4), (1,2,3,4)
Example 2 | 2 |()
Evens | 1,2,4,6,8,9 |(2,4,6), (4,6,8), (2,4,6,8)
Odds |-1,1,3,4,5,7,9,10|(-1,1,3), (3,4,5), (5,7,9)
Decreasing|10,7,4,3,2,1 |(10,7,4), (4,3,2), (3,2,1), (4,3,2,1)
Up & down |-1,1,3,2,1,0 |(-1,1,3), (3,2,1), (2,1,0), (3,2,1,0)
Unchanging| 0,42,42,42,42,17|(42,42,42), (42,42,42), (42,42,42,42)
END
}
###############################################################################
|