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
|
use v6d;
###############################################################################
=begin comment
Perl Weekly Challenge 196
=========================
TASK #2
-------
*Range List*
Submitted by: Mohammad S Anwar
You are given a sorted unique integer array, @array.
Write a script to find all possible Number Range i.e [x, y] represent range all
integers from x and y (both inclusive).
Each subsequence of two or more contiguous integers
Example 1
Input: @array = (1,3,4,5,7)
Output: [3,5]
Example 2
Input: @array = (1,2,3,6,7,9)
Output: [1,3], [6,7]
Example 3
Input: @array = (0,1,2,4,5,6,8,9)
Output: [0,2], [4,6], [8,9]
=end comment
###############################################################################
#--------------------------------------#
# Copyright © 2022 PerlMonk Athanasius #
#--------------------------------------#
#==============================================================================
=begin comment
Interface
---------
1. If no command-line arguments are given, the test suite is run.
2. If the first argument is negative, it must be preceded by "--" to distin-
guish it from a command-line flag.
=end comment
#==============================================================================
use Test;
my UInt constant $TEST-FIELDS = 3;
#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
"\nChallenge 196, Task #2: Range List (Raku)\n".put;
}
#==============================================================================
multi sub MAIN
(
*@array where array-is-valid( @array ) #= A list of 1 or more integers
)
#==============================================================================
{
"Input: \@array = (%s)\n".printf: @array.join: ',';
my Array[Int] @ranges = find-ranges( @array );
"Output: %s\n".printf:
find-ranges( @array ).map( { '[' ~ @$_.join( ',' ) ~ ']' } ).join: ', ';
}
#==============================================================================
multi sub MAIN() # Run the test suite
#==============================================================================
{
run-tests();
}
#------------------------------------------------------------------------------
sub find-ranges( List:D[Int:D] $array --> List:D[List:D[Int:D]] )
#------------------------------------------------------------------------------
{
my (Array[Int] @ranges, Int @range);
for @$array -> Int $n
{
if @range.elems == 0 || # Start the first range
$n == @range[ *-1 ] + 1 # Extend an existing range
{
@range.push: $n;
}
else # Start a new range
{
# First, handle (either keep or discard) the previous range
@ranges.push: @range.clone if @range.elems > 1;
@range = $n;
}
}
@ranges.push: @range if @range.elems > 1; # Handle the final range
@$_ = $_[ 0, *-1 ] for @ranges; # Remove all middle elements from ranges
return @ranges;
}
#------------------------------------------------------------------------------
sub array-is-valid( List:D[Int:D] $array --> Bool:D )
#------------------------------------------------------------------------------
{
return False unless $array.elems >= 1; # Array must not be empty
my UInt %integers{Int};
for @$array -> Int $n # Test whether array elements are unique
{
return False if ++%integers{ $n } > 1;
}
my Int @sorted = $array.sort; # Test whether array elements are sorted
return @sorted ~~ $array;
}
#------------------------------------------------------------------------------
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/;
my Int @array = $input .split( / \s+ /, :skip-empty )
.map: { .Int };
my Array[Int] @ranges = find-ranges( @array );
my Str $got = @ranges.map( { '[' ~ join( ',', @$_ ) ~ ']' } )
.join: ', ';
is $got, $expected, $test-name;
}
done-testing;
}
#------------------------------------------------------------------------------
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| 1 3 4 5 7 |[3,5]
Example 2| 1 2 3 6 7 9 |[1,3], [6,7]
Example 3| 0 1 2 4 5 6 8 9|[0,2], [4,6], [8,9]
Negatives|-3 -2 -1 1 2 5 6 |[-3,-1], [1,2], [5,6]
END
}
###############################################################################
|