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
|
' 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"] ]
dim shared names() as string
dim shared emails() as string
sub collect_args()
dim i as integer, j as integer, row as integer
row=1
i=1
do while command(i)<>""
redim preserve names(row) as string
redim preserve emails(row) as string
names(row)=command(i)
i=i+1
do while command(i)<>"" and command(i)<>","
emails(row)=emails(row)+command(i)+" "
i=i+1
loop
if command(i)="," then i=i+1: row=row+1
loop
end sub
sub split(byval text as string, delim as string = " ", ret() as string)
dim p as integer, count as integer
count=0
do
do while left(text,1)=delim: text=mid(text,2): loop ' eat front delimiters
if text="" then exit do
redim preserve ret(count) as string
p=instr(text,delim)
if p<1 then
ret(count)=text
exit do
else
ret(count)=left(text,p-1)
text=mid(text,p+1)
count=count+1
end if
loop
end sub
function common_email(byref aa as integer, byref bb as integer) as boolean
dim emails_a() as string, emails_b() as string
dim a as integer, i as integer, b as integer, j as integer
for a=0 to ubound(names)
split(emails(a),,emails_a())
for i=0 to ubound(emails_a)
for b=0 to ubound(names)
if a<>b then
split(emails(b),,emails_b())
for j=0 to ubound(emails_b)
if emails_a(i)=emails_b(j) then
aa=a: bb=b: common_email=true
exit function
end if
next
end if
next
next
next
common_email=false
end function
sub sort(s() as string)
dim i as integer, j as integer, tmp as string
for i=0 to ubound(s)-1
for j=i+1 to ubound(s)
if s(i)>s(j) then tmp=s(i): s(i)=s(j): s(j)=tmp
next
next
end sub
function sort_uniq_emails(emails as string) as string
dim s() as string, i as integer, last as string, ret as string
split emails,,s()
sort s()
ret=""
for i=0 to ubound(s)
if s(i)<>last then
ret=ret+s(i)+" "
last=s(i)
end if
next
sort_uniq_emails=ret
end function
sub merge_account(a as integer, b as integer)
dim all_emails as string, i as integer
all_emails=sort_uniq_emails(emails(a)+" "+emails(b))
emails(a)=all_emails
for i=b to ubound(names)-1
names(i)=names(i+1)
emails(i)=emails(i+1)
next
redim preserve names(ubound(names)-1)
redim preserve emails(ubound(emails)-1)
end sub
sub merge_accounts()
dim a as integer, b as integer
do while common_email(a, b)
merge_account(a, b)
loop
end sub
dim i as integer
collect_args
merge_accounts
for i=0 to ubound(names)
print names(i);" ";emails(i)
next
|