diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-20 18:41:45 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-20 18:41:45 +0000 |
| commit | 05a0862dc464bcdda8eaca308559334c2a4faebc (patch) | |
| tree | dd8873eee8f239ce1a1d6bc4c8bfa7bfeea771bb | |
| parent | 019010d917c7006777dab0bf252b5a2a72c077b4 (diff) | |
| download | perlweeklychallenge-club-05a0862dc464bcdda8eaca308559334c2a4faebc.tar.gz perlweeklychallenge-club-05a0862dc464bcdda8eaca308559334c2a4faebc.tar.bz2 perlweeklychallenge-club-05a0862dc464bcdda8eaca308559334c2a4faebc.zip | |
incomplete
| -rw-r--r-- | challenge-209/paulo-custodio/basic/ch-1.bas | 54 | ||||
| -rw-r--r-- | challenge-209/paulo-custodio/basic/ch-2.bas | 90 |
2 files changed, 144 insertions, 0 deletions
diff --git a/challenge-209/paulo-custodio/basic/ch-1.bas b/challenge-209/paulo-custodio/basic/ch-1.bas new file mode 100644 index 0000000000..fb9a6300d9 --- /dev/null +++ b/challenge-209/paulo-custodio/basic/ch-1.bas @@ -0,0 +1,54 @@ +' Challenge 209 +' +' Task 1: Special Bit Characters +' Submitted by: Mohammad S Anwar +' +' You are given an array of binary bits that ends with 0. +' +' Valid sequences in the bit string are: +' +' [0] -decodes-to-> "a" +' [1, 0] -> "b" +' [1, 1] -> "c" +' +' Write a script to print 1 if the last character is an “a” otherwise print 0. +' Example 1 +' +' Input: @bits = (1, 0, 0) +' Output: 1 +' +' The given array bits can be decoded as 2-bits character (10) followed by +' 1-bit character (0). +' +' Example 2 +' +' Input: @bits = (1, 1, 1, 0) +' Output: 0 +' +' Possible decode can be 2-bits character (11) followed by 2-bits character +' (10) i.e. the last character is not 1-bit character. + +function decode(in as string) as string + dim result as string + do while in<>"" + if left(in,1)="0" then + result=result+"a" + in=mid(in,2) + elseif left(in,2)="10" then + result=result+"b" + in=mid(in,3) + elseif left(in,2)="11" then + result=result+"c" + in=mid(in,3) + else + print "Error: cannot parse: "; in + end + end if + loop + decode=result +end function + +dim in as string, result as string +in=command(1) +result=decode(in) +if right(result,1)="a" then print 1: else print 0 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 |
