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
|
#!perl
################################################################################
=comment
Perl Weekly Challenge 209
=========================
TASK #2
-------
*Merge Account*
Submitted by: Mohammad S Anwar
You are given an array of accounts i.e. name with list of email addresses.
Write a script to merge the accounts where possible. The accounts can only be
merged if they have at least one email address in common.
Example 1:
Input: @accounts = [ ["A", "a1@a.com", "a2@a.com"],
["B", "b1@b.com"],
["A", "a3@a.com", "a1@a.com"] ]
]
Output: [ ["A", "a1@a.com", "a2@a.com", "a3@a.com"],
["B", "b1@b.com"] ]
Example 2:
Input: @accounts = [ ["A", "a1@a.com", "a2@a.com"],
["B", "b1@b.com"],
["A", "a3@a.com"],
["B", "b2@b.com", "b1@b.com"] ]
Output: [ ["A", "a1@a.com", "a2@a.com"],
["A", "a3@a.com"],
["B", "b1@b.com", "b2@b.com"] ]
=cut
################################################################################
#--------------------------------------#
# Copyright © 2023 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=comment
Interface
---------
If no command-line arguments are given, the test suite is run.
Assumptions
-----------
1. Accounts OF THE SAME NAME can be merged only if they have at least one email
address in common.
2. "Having an email address in common" is a transitive relation, so two accounts
with the same name but with no addresses in common can be merged if each has
an address in common with (say) a third account of the same name.
Note
----
No attempt is made to verify the format of email addresses.
=cut
#===============================================================================
use strict;
use warnings;
use Const::Fast;
use Set::Scalar;
use Test::More;
use Text::CSV;
const my $INDENT => ' ' x 8;
const my $USAGE =>
"Usage:
perl $0 <file>
perl $0
<file> Name of accounts file\n";
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 209, Task #2: Merge Account (Perl)\n\n";
}
#===============================================================================
MAIN:
#===============================================================================
{
my $args = scalar @ARGV;
if ($args == 0)
{
run_tests();
}
elsif ($args == 1)
{
my $accounts = read_accounts_file( $ARGV[ 0 ] );
print format_accounts( "Input: \@accounts", $accounts ), "\n";
my $merged = merge_accounts( $accounts );
print format_accounts( "Output: \@merged", $merged );
}
else
{
error( "Expected 1 or 0 command line arguments, found $args" );
}
}
#-------------------------------------------------------------------------------
sub merge_accounts
#-------------------------------------------------------------------------------
{
my ($accounts) = @_;
my (%name2accounts, @merged);
for my $account (@$accounts)
{
my ($name, @addresses) = @$account;
push @{ $name2accounts{ $name } }, \@addresses;
}
for my $name (sort keys %name2accounts)
{
L_LOOP:
{
my $accounts = $name2accounts{ $name };
for my $i (0 .. $#$accounts - 1)
{
my $s = Set::Scalar->new( @{ $accounts->[ $i ] } );
for my $j ($i + 1 .. $#$accounts)
{
my $t = Set::Scalar->new( @{ $accounts->[ $j ] } );
my $int = $s->intersection( $t );
if ($int->size > 0)
{
$accounts->[ $i ] = [ sort $s->union( $t )->members ];
splice @$accounts, $j, 1;
redo L_LOOP;
}
}
}
push @merged, [ $name, @$_ ] for @$accounts;
}
}
return \@merged;
}
#-------------------------------------------------------------------------------
sub read_accounts_file
#-------------------------------------------------------------------------------
{
my ($filename) = @_;
-e $filename or error( qq[File "$filename" not found] );
my @accounts;
my $csv = Text::CSV->new(
{
allow_loose_quotes => 1,
auto_diag => 1,
binary => 1,
}
);
open( my $fh, '<', $filename )
or die "Cannot open file $filename for reading, stopped";
while (my $row = $csv->getline( $fh ))
{
push @accounts, $row;
}
close $fh
or die "Cannot close file $filename, stopped";
return \@accounts;
}
#-------------------------------------------------------------------------------
sub format_accounts
#-------------------------------------------------------------------------------
{
my ($header, $accounts) = @_;
return sprintf "$header =\n%s[\n%s\n%s]\n",
$INDENT,
join
(
",\n",
map
{
$INDENT . ' [' . join( ', ', map { qq["$_"] } @$_ ) . ']'
} @$accounts
),
$INDENT;
}
#-------------------------------------------------------------------------------
sub run_tests
#-------------------------------------------------------------------------------
{
print "Running the test suite\n";
while (my $line = <DATA>)
{
chomp $line;
my ($test_name, $in_file, $out_file) = split / \| /x, $line;
my $got = merge_accounts( read_accounts_file( $in_file ) );
my $expected = read_accounts_file( $out_file );
is_deeply $got, $expected, $test_name;
}
done_testing;
}
#-------------------------------------------------------------------------------
sub error
#-------------------------------------------------------------------------------
{
my ($message) = @_;
die "ERROR: $message\n$USAGE";
}
################################################################################
__DATA__
Example 1 |Example_1.txt |Example_1_answer.txt
Example 2 |Example_2.txt |Example_2_answer.txt
Transitive|Transitive.txt|Transitive_answer.txt
|