/
ch-2.pl
191 lines (147 loc) · 4.81 KB
/
ch-2.pl
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
#!perl
################################################################################
=comment
Perl Weekly Challenge 229
=========================
TASK #2
-------
*Two out of Three*
Submitted by: Mohammad S Anwar
You are given three array of integers.
Write a script to return all the elements that are present in at least 2 out of
3 given arrays.
Example 1
Input: @array1 = (1, 1, 2, 4)
@array2 = (2, 4)
@array3 = (4)
Output: (2, 4)
Example 2
Input: @array1 = (4, 1)
@array2 = (2, 4)
@array3 = (1, 2)
Output: (1, 2, 4)
=cut
################################################################################
#--------------------------------------#
# Copyright © 2023 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=comment
Interface
---------
If no command-line arguments are given, the test suite is run.
=cut
#===============================================================================
use strict;
use warnings;
use Const::Fast;
use List::Util qw( uniqint );
use Regexp::Common qw( number );
use Test::More;
const my $USAGE =>
"Usage:
perl $0 <array1> <array2> <array3>
perl $0
<array1> A string of comma-separated integers
<array2> A string of comma-separated integers
<array3> A string of comma-separated integers\n";
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 229, Task #2: Two out of Three (Perl)\n\n";
}
#===============================================================================
MAIN:
#===============================================================================
{
my $args = scalar @ARGV;
if ($args == 0)
{
run_tests();
}
elsif ($args == 3)
{
my $array1 = parse_int_str( $ARGV[ 0 ] );
my $array2 = parse_int_str( $ARGV[ 1 ] );
my $array3 = parse_int_str( $ARGV[ 2 ] );
printf "Input: \@array1 = (%s)\n", join ', ', @$array1;
printf " \@array2 = (%s)\n", join ', ', @$array2;
printf " \@array3 = (%s)\n", join ', ', @$array3;
my $shared = find_shared_elements( $array1, $array2, $array3 );
printf "Output: (%s)\n", join ', ', @$shared;
}
else
{
error( "Expected 0 or 3 command-line arguments, found $args" );
}
}
#-------------------------------------------------------------------------------
sub find_shared_elements
#-------------------------------------------------------------------------------
{
my ($array1, $array2, $array3) = @_;
my @shared;
my %count;
++$count{ $_ }
for uniqint( @$array1 ), uniqint( @$array2 ), uniqint( @$array3 );
for my $key (keys %count)
{
push @shared, $key if $count{ $key } >= 2;
}
@shared = sort @shared;
return \@shared;
}
#-------------------------------------------------------------------------------
sub parse_int_str
#-------------------------------------------------------------------------------
{
my ($str) = @_;
my @elems = split / \s* \, \s* /x, $str;
my @ints;
for my $elem (@elems)
{
$elem =~ / ^ $RE{num}{int} $ /x
or error( qq["$elem" is not a valid integer] );
push @ints, $elem;
}
return \@ints;
}
#-------------------------------------------------------------------------------
sub run_tests
#-------------------------------------------------------------------------------
{
print "Running the test suite\n";
while (my $line = <DATA>)
{
chomp $line;
my ($test_name, $array1_str, $array2_str, $array3_str, $expctd_str) =
split / \| /x, $line;
for ($test_name, $array1_str, $array2_str, $array3_str, $expctd_str)
{
s/ ^ \s+ //x;
s/ \s+ $ //x;
}
my @array1 = split / \s+ /x, $array1_str;
my @array2 = split / \s+ /x, $array2_str;
my @array3 = split / \s+ /x, $array3_str;
my @expctd = split / \s+ /x, $expctd_str;
my $shared = find_shared_elements( \@array1, \@array2, \@array3 );
is_deeply $shared, \@expctd, $test_name;
}
done_testing;
}
#-------------------------------------------------------------------------------
sub error
#-------------------------------------------------------------------------------
{
my ($message) = @_;
die "ERROR: $message\n$USAGE";
}
################################################################################
__DATA__
Example 1| 1 1 2 4 | 2 4 |4 | 2 4
Example 2| 4 1 | 2 4 |1 2 | 1 2 4
Negatives|-1 7 0 5 12|-5 -12 -1 17 4|0 32 |-1 0
Disjoint | 1 2 3 4 |-1 -2 -3 -4 |0 5 -5|