aboutsummaryrefslogtreecommitdiff
path: root/challenge-209/paulo-custodio/basic/ch-2.bas
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2023-03-20 18:41:45 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2023-03-20 18:41:45 +0000
commit05a0862dc464bcdda8eaca308559334c2a4faebc (patch)
treedd8873eee8f239ce1a1d6bc4c8bfa7bfeea771bb /challenge-209/paulo-custodio/basic/ch-2.bas
parent019010d917c7006777dab0bf252b5a2a72c077b4 (diff)
downloadperlweeklychallenge-club-05a0862dc464bcdda8eaca308559334c2a4faebc.tar.gz
perlweeklychallenge-club-05a0862dc464bcdda8eaca308559334c2a4faebc.tar.bz2
perlweeklychallenge-club-05a0862dc464bcdda8eaca308559334c2a4faebc.zip
incomplete
Diffstat (limited to 'challenge-209/paulo-custodio/basic/ch-2.bas')
-rw-r--r--challenge-209/paulo-custodio/basic/ch-2.bas90
1 files changed, 90 insertions, 0 deletions
diff --git a/challenge-209/paulo-custodio/basic/ch-2.bas b/challenge-209/paulo-custodio/basic/ch-2.bas
new file mode 100644
index 0000000000..b73570d522
--- /dev/null
+++ b/challenge-209/paulo-custodio/basic/ch-2.bas
@@ -0,0 +1,90 @@
+' 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 a as integer, byref b as integer) as boolean
+ dim emails_a() as string, emails_b() as string
+ dim i as integer, j as integer, k as integer
+ for i=0 to ubound(names)
+ split(emails(i),,emails_a())
+ print names(i);" ";
+ for j=0 to ubound(emails_a): print emails_a(j);" ";: next
+ print
+ next
+ common_email=false
+end function
+
+
+dim i as integer, a as integer, b as integer
+
+collect_args
+if common_email(a, b) then print a, b
+for i=0 to ubound(names)
+ print i;names(i) ',emails(i)
+next