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
|
#!perl
###############################################################################
=comment
Perl Weekly Challenge 131
=========================
TASK #1
-------
*Consecutive Arrays*
Submitted by: Mark Anderson
You are given a sorted list of unique positive integers.
Write a script to return list of arrays where the arrays are consecutive
integers.
Example 1:
Input: (1, 2, 3, 6, 7, 8, 9)
Output: ([1, 2, 3], [6, 7, 8, 9])
Example 2:
Input: (11, 12, 14, 17, 18, 19)
Output: ([11, 12], [14], [17, 18, 19])
Example 3:
Input: (2, 4, 6, 8)
Output: ([2], [4], [6], [8])
Example 4:
Input: (1, 2, 3, 4, 5)
Output: ([1, 2, 3, 4, 5])
=cut
###############################################################################
#--------------------------------------#
# Copyright © 2021 PerlMonk Athanasius #
#--------------------------------------#
#==============================================================================
=comment
Note
----
The input must be a list of unsigned integers. Any duplicates will be silently
removed, and the list will be sorted in increasing numerical order, before it
is processed.
=cut
#==============================================================================
use strict;
use warnings;
use Const::Fast;
use List::Util qw( uniqint );
use Regexp::Common qw( number );
const my $USAGE =>
"Usage:
perl $0 [<list> ...]
[<list> ...] A list of unsigned integers\n";
#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 131, Task #1: Consecutive Arrays (Perl)\n\n";
}
#==============================================================================
MAIN:
#==============================================================================
{
my @list = parse_command_line();
my @sorted = sort { $a <=> $b } uniqint @list;
printf "Input: (%s)\n", join ', ', @sorted;
my @consec = get_consecutive_arrays( @sorted );
printf "Output: (%s)\n",
join ', ', map { '[' . join( ', ', @$_ ) . ']' } @consec;
}
#------------------------------------------------------------------------------
sub get_consecutive_arrays
#------------------------------------------------------------------------------
{
my @sorted = @_;
my (@consec, @range, $last);
for my $value (@sorted)
{
if (!defined( $last ) || $last == $value - 1)
{
push @range, $value;
}
else
{
push @consec, [ @range ]; # Save a copy of the current range
@range = $value; # Begin the next range
}
$last = $value;
}
push @consec, \@range;
return @consec;
}
#------------------------------------------------------------------------------
sub parse_command_line
#------------------------------------------------------------------------------
{
for (@ARGV)
{
/ ^ $RE{num}{int} $ /x
or error( qq["$_" is not a valid integer] );
$_ >= 0
or error( qq["$_" is not a positive integer] );
}
return @ARGV;
}
#------------------------------------------------------------------------------
sub error
#------------------------------------------------------------------------------
{
my ($message) = @_;
die "\nERROR: $message\n$USAGE";
}
###############################################################################
|