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
|
#!perl
################################################################################
=comment
Perl Weekly Challenge 282
=========================
TASK #1
-------
*Good Integer*
Submitted by: Mohammad Sajid Anwar
You are given a positive integer, $int, having 3 or more digits.
Write a script to return the Good Integer in the given integer or -1 if none
found.
A good integer is exactly three consecutive matching digits.
Example 1
Input: $int = 12344456
Output: "444"
Example 2
Input: $int = 1233334
Output: -1
Example 3
Input: $int = 10020003
Output: "000"
=cut
################################################################################
#--------------------------------------#
# Copyright © 2024 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=comment
Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. A positive integer of 3 or more digits is entered on the command-line.
Assumptions
-----------
1. Since the input integer must have at least 3 digits, and valid integers do
not have leading zeros, the smallest valid input integer is 100.
2. A leading plus ("+") sign is also disallowed: only the digits 0-9 are accept-
ed.
=cut
#===============================================================================
use v5.32; # Enables strictures
use warnings;
use Const::Fast;
use Test::More;
const my $USAGE => <<END;
Usage:
perl $0 <int>
perl $0
<int> A positive integer of 3 or more digits
END
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 282, Task #1: Good Integer (Perl)\n\n";
}
#===============================================================================
MAIN:
#===============================================================================
{
my $argc = scalar @ARGV;
if ($argc == 0)
{
run_tests();
}
elsif ($argc == 1)
{
my $int = $ARGV[ 0 ];
$int =~ / ^ [1-9] \d{2,} /x
or error( qq["$int" is not a valid input integer] );
print "Input: \$int = $int\n";
my $good_int = find_good_integer( $int );
print "Output: $good_int\n";
}
else
{
error( "Expected 1 or 0 command-line arguments, found $argc" );
}
}
#-------------------------------------------------------------------------------
sub find_good_integer
#-------------------------------------------------------------------------------
{
my ($int) = @_;
my @runs = grep { length $_ == 3 } $int =~ / ( (\d) \2{2,} ) /gx;
return scalar @runs ? qq["$runs[ 0 ]"] : -1;
}
#-------------------------------------------------------------------------------
sub run_tests
#-------------------------------------------------------------------------------
{
print "Running the test suite\n";
while (my $line = <DATA>)
{
chomp $line;
my ($test_name, $int, $expected) = split / \| /x, $line;
for ($test_name, $int, $expected)
{
s/ ^ \s+ //x;
s/ \s+ $ //x;
}
my $good_int = find_good_integer( $int );
is $good_int, $expected, $test_name;
}
done_testing;
}
#-------------------------------------------------------------------------------
sub error
#-------------------------------------------------------------------------------
{
my ($message) = @_;
die "ERROR: $message\n$USAGE";
}
################################################################################
__DATA__
Example 1 |12344456 |"444"
Example 2 |1233334 |-1
Example 3 |10020003 |"000"
Multiple |122234445666 |"222"
Initial |9992555 |"999"
Final |123776666888 |"888"
Final 4 |123456789999 |-1
All too long|111112222333334444|-1
Between 4s |122223334444567 |"333"
|