aboutsummaryrefslogtreecommitdiff
path: root/challenge-092
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-03-07 07:01:41 +0000
committerGitHub <noreply@github.com>2021-03-07 07:01:41 +0000
commita93b6ae2bca9e39ac76b4c5e2879b349940731b0 (patch)
tree5dcb6ba807159563e1fd082c81694a670479c29c /challenge-092
parentb36412240457bb8e41e5fdc7c912b084a3afd4df (diff)
parent6ba83654820bad00ba3c679bbe18764269dabe0f (diff)
downloadperlweeklychallenge-club-a93b6ae2bca9e39ac76b4c5e2879b349940731b0.tar.gz
perlweeklychallenge-club-a93b6ae2bca9e39ac76b4c5e2879b349940731b0.tar.bz2
perlweeklychallenge-club-a93b6ae2bca9e39ac76b4c5e2879b349940731b0.zip
Merge pull request #3663 from pauloscustodio/paulo-custodio
Paulo Custodio
Diffstat (limited to 'challenge-092')
-rw-r--r--challenge-092/paulo-custodio/basic/ch-1.bas47
-rw-r--r--challenge-092/paulo-custodio/basic/ch-2.bas66
-rw-r--r--challenge-092/paulo-custodio/perl/ch-1.pl3
-rw-r--r--challenge-092/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-092/paulo-custodio/t/test-2.yaml15
-rw-r--r--challenge-092/paulo-custodio/test.pl18
6 files changed, 146 insertions, 18 deletions
diff --git a/challenge-092/paulo-custodio/basic/ch-1.bas b/challenge-092/paulo-custodio/basic/ch-1.bas
new file mode 100644
index 0000000000..1e042022ff
--- /dev/null
+++ b/challenge-092/paulo-custodio/basic/ch-1.bas
@@ -0,0 +1,47 @@
+' Challenge 092
+'
+' TASK #1 › Isomorphic Strings
+' Submitted by: Mohammad S Anwar
+' You are given two strings $A and $B.
+'
+' Write a script to check if the given strings are Isomorphic. Print 1 if they
+' are otherwise 0.
+'
+' Example 1:
+' Input: $A = "abc"; $B = "xyz"
+' Output: 1
+' Example 2:
+' Input: $A = "abb"; $B = "xyy"
+' Output: 1
+' Example 3:
+' Input: $A = "sum"; $B = "add"
+' Output: 0
+
+function isomorphic(a as string, b as string) as integer
+ dim mapping(256) as integer, mapped(256) as integer
+ dim i as integer, ac as integer, bc as integer
+
+ if a="" or len(a)<>len(b) then
+ isomorphic = 0: exit function
+ end if
+
+ for i=1 to len(a)
+ ac = asc(mid(a,i,1))
+ bc = asc(mid(b,i,1))
+ if mapping(ac)=0 then ' a is new
+ if mapped(bc)<>0 then ' b already mapped to some other a
+ isomorphic = 0: exit function
+ else ' store mapping
+ mapping(ac) = bc
+ mapped(bc) = 1
+ end if
+ else ' a already occurred
+ if mapping(ac)<>bc then ' previous mapping is different
+ isomorphic = 0: exit function
+ end if
+ end if
+ next
+ isomorphic = 1
+end function
+
+print trim(str(isomorphic(command(1), command(2)))) \ No newline at end of file
diff --git a/challenge-092/paulo-custodio/basic/ch-2.bas b/challenge-092/paulo-custodio/basic/ch-2.bas
new file mode 100644
index 0000000000..a974817a64
--- /dev/null
+++ b/challenge-092/paulo-custodio/basic/ch-2.bas
@@ -0,0 +1,66 @@
+' Challenge 092
+'
+' TASK #2 › Insert Interval
+' Submitted by: Mohammad S Anwar
+' You are given a set of sorted non-overlapping intervals and a new interval.
+'
+' Write a script to merge the new interval to the given set of intervals.
+'
+' Example 1:
+' Input $S = (1,4), (8,10); $N = (2,6)
+' Output: (1,6), (8,10)
+' Example 2:
+' Input $S = (1,2), (3,7), (8,10); $N = (5,8)
+' Output: (1,2), (3,10)
+' Example 3:
+' Input $S = (1,5), (7,9); $N = (10,11)
+' Output: (1,5), (7,9), (10,11)
+
+redim shared timeline(1) as Boolean
+
+sub fill_timeline()
+ dim i as integer, j as integer, p as integer, bg as integer, ed as integer
+ i=1
+ do while command(i)<>""
+ ' parse begin,end
+ p=instr(command(i),",")
+ if p=0 then error 5
+ bg=val(command(i))
+ ed=val(mid(command(i),p+1))
+ ' resize timeline if needed
+ if 2*ed>ubound(timeline) then
+ redim preserve timeline(2*ed+2)
+ end if
+
+ ' fill interval
+ for j=2*bg to 2*ed
+ timeline(j)=true
+ next
+ i=i+1
+ loop
+end sub
+
+sub print_timeline()
+ dim i as integer
+ redim intervals(0) as integer
+
+ for i=lbound(timeline) to ubound(timeline)-1
+ if timeline(i)=false and timeline(i+1)=true then
+ redim preserve intervals(ubound(intervals)+1)
+ intervals(ubound(intervals))=int(i/2)+1
+ elseif timeline(i)=true and timeline(i+1)=false then
+ redim preserve intervals(ubound(intervals)+1)
+ intervals(ubound(intervals))=int(i/2)
+ end if
+ next
+
+ for i=1 to ubound(intervals) step 2
+ print "(";trim(str(intervals(i)));",";trim(str(intervals(i+1)));")";
+ if i+2<ubound(intervals) then print ", ";
+ next
+ print
+end sub
+
+' main
+fill_timeline
+print_timeline
diff --git a/challenge-092/paulo-custodio/perl/ch-1.pl b/challenge-092/paulo-custodio/perl/ch-1.pl
index 8416035e4d..49a153e10b 100644
--- a/challenge-092/paulo-custodio/perl/ch-1.pl
+++ b/challenge-092/paulo-custodio/perl/ch-1.pl
@@ -6,7 +6,8 @@
# Submitted by: Mohammad S Anwar
# You are given two strings $A and $B.
#
-# Write a script to check if the given strings are Isomorphic. Print 1 if they are otherwise 0.
+# Write a script to check if the given strings are Isomorphic. Print 1 if they
+# are otherwise 0.
#
# Example 1:
# Input: $A = "abc"; $B = "xyz"
diff --git a/challenge-092/paulo-custodio/t/test-1.yaml b/challenge-092/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..fadc975fa6
--- /dev/null
+++ b/challenge-092/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: abc xyz
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: abb xyy
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: sum add
+ input:
+ output: 0
diff --git a/challenge-092/paulo-custodio/t/test-2.yaml b/challenge-092/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..97da8a27bb
--- /dev/null
+++ b/challenge-092/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 1,4 8,10 2,6
+ input:
+ output: (1,6), (8,10)
+- setup:
+ cleanup:
+ args: 1,2 3,7 8,10 5,8
+ input:
+ output: (1,2), (3,10)
+- setup:
+ cleanup:
+ args: 1,5 7,9 10,11
+ input:
+ output: (1,5), (7,9), (10,11)
diff --git a/challenge-092/paulo-custodio/test.pl b/challenge-092/paulo-custodio/test.pl
index 6f6eddbaba..01ed2b83cd 100644
--- a/challenge-092/paulo-custodio/test.pl
+++ b/challenge-092/paulo-custodio/test.pl
@@ -2,22 +2,6 @@
use strict;
use warnings;
-use Test::More;
use 5.030;
-is capture("perl perl/ch-1.pl abc xyz"), "1\n";
-is capture("perl perl/ch-1.pl abb xyy"), "1\n";
-is capture("perl perl/ch-1.pl sum add"), "0\n";
-
-is capture("perl perl/ch-2.pl 1,4 8,10 2,6"), "(1,6), (8,10)\n";
-is capture("perl perl/ch-2.pl 1,2 3,7 8,10 5,8"), "(1,2), (3,10)\n";
-is capture("perl perl/ch-2.pl 1,5 7,9 10,11"), "(1,5), (7,9), (10,11)\n";
-
-done_testing;
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \t\v\f\r]*\n/\n/g;
- return $out;
-}
+require '../../challenge-001/paulo-custodio/test.pl';