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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
#!perl
################################################################################
=comment
Perl Weekly Challenge 277
=========================
TASK #2
-------
*Strong Pair*
Submitted by: Mohammad Sajid Anwar
You are given an array of integers, @ints.
Write a script to return the count of all strong pairs in the given array.
A pair of integers x and y is called strong pair if it satisfies:
0 < |x - y| < min(x, y).
Example 1
Input: @ints = (1, 2, 3, 4, 5)
Output: 4
Strong Pairs: (2, 3), (3, 4), (3, 5), (4, 5)
Example 2
Input: @ints = (5, 7, 1, 7)
Output: 1
Strong Pairs: (5, 7)
=cut
################################################################################
#--------------------------------------#
# Copyright © 2024 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=comment
Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. If an explanation of the output is required, the flag "--verbose" is entered
on the command-line.
3. The input integers are entered as a non-empty list at the end of the command-
line.
4. If any input integer is negative, the first such must be preceded by "--" to
indicate that it is not a command-line flag.
Assumption
----------
Within a strong pair (x, y), the order of x and y is not significant. So (x, y)
is the same strong pair as (y, x). For convenience, strong pairs are always
given as (x, y) where x < y (see Analysis below).
Analysis
--------
Requirements (given): (a) 0 < |x - y|
(b) 0 < min(x, y) (by transitivity)
(c) |x - y| < min(x, y)
1. Let d = |x - y|. If x = y, then d = 0; but from (a) we know that 0 < d, so it
follows that x ≠ y.
2. For convenience, let each strong pair (x, y) be ordered such that x < y.
Then min(x, y) = x.
3. From (2) together with (b) it follows that x > 0; and from (1) we know that
d > 0. But if x = 1, (c) is impossible; therefore, x > 1.
4. From (c) we have y - x < x. Adding x to both sides yields y < 2x.
Summary. For any strong pair (x, y) ordered so that x < y, it is required that:
(d) 1 < x < y < 2x
=cut
#===============================================================================
use v5.32.1; # Enables strictures
use warnings;
use Const::Fast;
use Getopt::Long;
use List::Util qw( uniqint );
use Regexp::Common qw( number );
use Test::More;
const my $USAGE => <<END;
Usage:
perl $0 [--verbose] [<ints> ...]
perl $0
--verbose Explain the output? [default: False]
[<ints> ...] A non-empty list of integers
END
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 277, Task #2: Strong Pair (Perl)\n\n";
}
#-------------------------------------------------------------------------------
package StrongPair
#-------------------------------------------------------------------------------
{
use Moo;
use Types::Standard qw( Int );
use namespace::clean;
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
has x =>
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(
is => 'ro',
isa => Int
);
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
has y =>
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(
is => 'ro',
isa => Int
);
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub BUILD # Sanity check
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
{
my ($self) = @_;
my $x = $self->{ x };
my $y = $self->{ y };
$x < $y && 0 < ($y - $x) < $x or die 'Invalid StrongPair';
}
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub fmt
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
{
my ($self) = @_;
my $x = $self->{ x };
my $y = $self->{ y };
return "($x, $y)";
}
}
#===============================================================================
MAIN:
#===============================================================================
{
if (scalar @ARGV == 0)
{
run_tests();
}
else
{
my ($verbose, $ints) = parse_command_line();
printf "Input: \@ints = (%s)\n", join ', ', @$ints;
my $pairs = find_strong_pairs( $ints );
my $count = scalar @$pairs;
print "Output: $count\n";
if ($verbose && $count > 0)
{
printf "\nStrong pair%s: %s\n",
$count == 1 ? '' : 's', join ', ', map { $_->fmt } @$pairs;
}
}
}
#-------------------------------------------------------------------------------
sub find_strong_pairs
#-------------------------------------------------------------------------------
{
my ($ints_arg) = @_;
my @ints = sort { $a <=> $b } uniqint grep { $_ > 1 } @$ints_arg;
my @pairs;
for my $i (0 .. $#ints - 1)
{
for my $j ($i + 1 .. $#ints)
{
my $x = $ints[ $i ];
my $y = $ints[ $j ];
if ($y < 2 * $x) # See requirement (d) in Analysis, above
{
push @pairs, StrongPair->new( x => $x, y => $y );
}
else
{
last;
}
}
}
return \@pairs;
}
#-------------------------------------------------------------------------------
sub parse_command_line
#-------------------------------------------------------------------------------
{
my $verbose = 0;
GetOptions
(
verbose => \$verbose
) or error( 'Invalid command-line argument' );
my @ints = @ARGV;
scalar @ints > 0 or error( 'Missing command-line input' );
for (@ints)
{
/ ^ $RE{num}{int} $ /x or error( qq["$_" is not a valid integer] );
}
return ($verbose, \@ints);
}
#-------------------------------------------------------------------------------
sub run_tests
#-------------------------------------------------------------------------------
{
print "Running the test suite\n";
while (my $line = <DATA>)
{
chomp $line;
my ($test_name, $ints_str, $exp_str) = split / \| /x, $line;
for ($test_name, $ints_str, $exp_str)
{
s/ ^ \s+ //x;
s/ \s+ $ //x;
}
my @ints = split / \s+ /x, $ints_str;
my $pairs = find_strong_pairs( \@ints );
my @exp_strs = split / \; \s* /x, $exp_str;
my @expected;
for my $str (@exp_strs)
{
my ($x, $y) = split / \s+ /x, $str;
push @expected, StrongPair->new( x => $x, y => $y );
}
is_deeply $pairs, \@expected, $test_name;
}
done_testing;
}
#-------------------------------------------------------------------------------
sub error
#-------------------------------------------------------------------------------
{
my ($message) = @_;
die "ERROR: $message\n$USAGE";
}
################################################################################
__DATA__
Example 1|1 2 3 4 5|2 3; 3 4; 3 5; 4 5
Example 2|5 7 1 7 |5 7
|