diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-03-09 20:01:18 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-03-09 20:01:18 +0000 |
| commit | c51f9bd979dbd79f20cb0abd543dc9b4df9f4e84 (patch) | |
| tree | 9133aa3fc3d107c7b44de3905fc7b1d2ab307aaa /challenge-092 | |
| parent | bfa8b8a3dda2ac37c22c7f4fc2867211f92f65e0 (diff) | |
| download | perlweeklychallenge-club-c51f9bd979dbd79f20cb0abd543dc9b4df9f4e84.tar.gz perlweeklychallenge-club-c51f9bd979dbd79f20cb0abd543dc9b4df9f4e84.tar.bz2 perlweeklychallenge-club-c51f9bd979dbd79f20cb0abd543dc9b4df9f4e84.zip | |
Remove tabs
Diffstat (limited to 'challenge-092')
| -rw-r--r-- | challenge-092/paulo-custodio/basic/ch-1.bas | 52 | ||||
| -rw-r--r-- | challenge-092/paulo-custodio/basic/ch-2.bas | 74 | ||||
| -rw-r--r-- | challenge-092/paulo-custodio/lua/ch-1.lua | 54 | ||||
| -rw-r--r-- | challenge-092/paulo-custodio/lua/ch-2.lua | 62 | ||||
| -rw-r--r-- | challenge-092/paulo-custodio/python/ch-1.py | 32 | ||||
| -rw-r--r-- | challenge-092/paulo-custodio/python/ch-2.py | 2 |
6 files changed, 138 insertions, 138 deletions
diff --git a/challenge-092/paulo-custodio/basic/ch-1.bas b/challenge-092/paulo-custodio/basic/ch-1.bas index 1e042022ff..bfaafba85f 100644 --- a/challenge-092/paulo-custodio/basic/ch-1.bas +++ b/challenge-092/paulo-custodio/basic/ch-1.bas @@ -4,7 +4,7 @@ ' 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 +' Write a script to check if the given strings are Isomorphic. Print 1 if they ' are otherwise 0. ' ' Example 1: @@ -18,30 +18,30 @@ ' 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 + 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 +print trim(str(isomorphic(command(1), command(2)))) diff --git a/challenge-092/paulo-custodio/basic/ch-2.bas b/challenge-092/paulo-custodio/basic/ch-2.bas index a974817a64..dc292b2769 100644 --- a/challenge-092/paulo-custodio/basic/ch-2.bas +++ b/challenge-092/paulo-custodio/basic/ch-2.bas @@ -19,46 +19,46 @@ 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 + 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 + 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 diff --git a/challenge-092/paulo-custodio/lua/ch-1.lua b/challenge-092/paulo-custodio/lua/ch-1.lua index d93a4c24ae..335b640620 100644 --- a/challenge-092/paulo-custodio/lua/ch-1.lua +++ b/challenge-092/paulo-custodio/lua/ch-1.lua @@ -22,36 +22,36 @@ Output: 0 --]] function split_string(str) - local t = {} - string.gsub(str, ".", function(c) table.insert(t,c) end) - return t + local t = {} + string.gsub(str, ".", function(c) table.insert(t,c) end) + return t end function isomorphic(a, b) - if #a ~= #b then - return 0 - else - local a = split_string(a) - local b = split_string(b) - local mapping = {} - local mapped = {} - - for i=1, #a do - if mapping[a[i]] == nil then -- a is new - if mapped[b[i]] ~= nil then -- b already mapped to some other a - return 0 - else -- store mapping - mapping[a[i]] = b[i] - mapped[b[i]] = 1 - end - else -- a already occurred - if mapping[a[i]] ~= b[i] then -- previous mapping is different - return 0 - end - end - end - return 1 - end + if #a ~= #b then + return 0 + else + local a = split_string(a) + local b = split_string(b) + local mapping = {} + local mapped = {} + + for i=1, #a do + if mapping[a[i]] == nil then -- a is new + if mapped[b[i]] ~= nil then -- b already mapped to some other a + return 0 + else -- store mapping + mapping[a[i]] = b[i] + mapped[b[i]] = 1 + end + else -- a already occurred + if mapping[a[i]] ~= b[i] then -- previous mapping is different + return 0 + end + end + end + return 1 + end end io.write(isomorphic(arg[1], arg[2])) diff --git a/challenge-092/paulo-custodio/lua/ch-2.lua b/challenge-092/paulo-custodio/lua/ch-2.lua index a88de5aace..5df3153e72 100644 --- a/challenge-092/paulo-custodio/lua/ch-2.lua +++ b/challenge-092/paulo-custodio/lua/ch-2.lua @@ -23,40 +23,40 @@ Output: (1,5), (7,9), (10,11) timeline = {} function fill_timeline() - for i=1,#arg do - -- parse begin,end - local bg, ed = string.match(arg[i], "(%d+),(%d+)") - bg = tonumber(bg) - ed = tonumber(ed) - - -- resize timeline if needed - while 2*ed >= #timeline do - table.insert(timeline, false) - end - - -- fill interval - for j=2*bg, 2*ed do - timeline[j] = true - end - end + for i=1,#arg do + -- parse begin,end + local bg, ed = string.match(arg[i], "(%d+),(%d+)") + bg = tonumber(bg) + ed = tonumber(ed) + + -- resize timeline if needed + while 2*ed >= #timeline do + table.insert(timeline, false) + end + + -- fill interval + for j=2*bg, 2*ed do + timeline[j] = true + end + end end function print_timeline() - -- collect intervals - local intervals = {} - for i=1, #timeline-1 do - if timeline[i] == false and timeline[i+1] == true then - table.insert(intervals, math.floor(i/2)+1) - elseif timeline[i] == true and timeline[i+1] == false then - table.insert(intervals, math.floor(i/2)) - end - end - - -- print intervals - for i=1, #intervals, 2 do - io.write("(",intervals[i],",",intervals[i+1],")") - if i+2 < #intervals then io.write(", ") end - end + -- collect intervals + local intervals = {} + for i=1, #timeline-1 do + if timeline[i] == false and timeline[i+1] == true then + table.insert(intervals, math.floor(i/2)+1) + elseif timeline[i] == true and timeline[i+1] == false then + table.insert(intervals, math.floor(i/2)) + end + end + + -- print intervals + for i=1, #intervals, 2 do + io.write("(",intervals[i],",",intervals[i+1],")") + if i+2 < #intervals then io.write(", ") end + end end fill_timeline() diff --git a/challenge-092/paulo-custodio/python/ch-1.py b/challenge-092/paulo-custodio/python/ch-1.py index 73b5d30b43..70e334812e 100644 --- a/challenge-092/paulo-custodio/python/ch-1.py +++ b/challenge-092/paulo-custodio/python/ch-1.py @@ -22,21 +22,21 @@ import sys def isomorphic(a,b): - if len(a)!=len(b): - return 0 - else: - mapping = {} - mapped = {} - for i in range(0, len(a)): - if not a[i] in mapping: # a is new - if b[i] in mapped: # b already mapped to some other a - return 0 - else: # store mapping - mapping[a[i]] = b[i] - mapped[b[i]] = 1 - else: # a already occurred - if mapping[a[i]]!=b[i]: # previous mapping is different - return 0 - return 1 + if len(a)!=len(b): + return 0 + else: + mapping = {} + mapped = {} + for i in range(0, len(a)): + if not a[i] in mapping: # a is new + if b[i] in mapped: # b already mapped to some other a + return 0 + else: # store mapping + mapping[a[i]] = b[i] + mapped[b[i]] = 1 + else: # a already occurred + if mapping[a[i]]!=b[i]: # previous mapping is different + return 0 + return 1 print(isomorphic(sys.argv[1], sys.argv[2])) diff --git a/challenge-092/paulo-custodio/python/ch-2.py b/challenge-092/paulo-custodio/python/ch-2.py index bf8cf623cb..70338a0976 100644 --- a/challenge-092/paulo-custodio/python/ch-2.py +++ b/challenge-092/paulo-custodio/python/ch-2.py @@ -74,5 +74,5 @@ def intervals_str(intervals): if i<len(intervals)-1: out += ', ' return out - + print(intervals_str(add_intervals())) |
