diff options
| author | 冯昶 <seaker@qq.com> | 2021-03-15 18:18:09 +0800 |
|---|---|---|
| committer | 冯昶 <seaker@qq.com> | 2021-03-15 18:18:09 +0800 |
| commit | 5ed25077fde85262036c9db3e893d70ae0907b5c (patch) | |
| tree | 8932d25b3fa6076e2d91ab2a331d4d8bfff20544 /challenge-092/paulo-custodio/lua | |
| parent | 8b6be37fe4dac8b4c6489a95e55514b76b298d15 (diff) | |
| parent | 65d54d52500028ec5359a7d39619803ade281543 (diff) | |
| download | perlweeklychallenge-club-5ed25077fde85262036c9db3e893d70ae0907b5c.tar.gz perlweeklychallenge-club-5ed25077fde85262036c9db3e893d70ae0907b5c.tar.bz2 perlweeklychallenge-club-5ed25077fde85262036c9db3e893d70ae0907b5c.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-092/paulo-custodio/lua')
| -rw-r--r-- | challenge-092/paulo-custodio/lua/ch-1.lua | 57 | ||||
| -rw-r--r-- | challenge-092/paulo-custodio/lua/ch-2.lua | 63 |
2 files changed, 120 insertions, 0 deletions
diff --git a/challenge-092/paulo-custodio/lua/ch-1.lua b/challenge-092/paulo-custodio/lua/ch-1.lua new file mode 100644 index 0000000000..335b640620 --- /dev/null +++ b/challenge-092/paulo-custodio/lua/ch-1.lua @@ -0,0 +1,57 @@ +#!/usr/bin/env lua + +--[[ +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 split_string(str) + 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 +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 new file mode 100644 index 0000000000..5df3153e72 --- /dev/null +++ b/challenge-092/paulo-custodio/lua/ch-2.lua @@ -0,0 +1,63 @@ +#!/usr/bin/env lua + +--[[ +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) +--]] + +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 +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 +end + +fill_timeline() +print_timeline() |
