diff options
Diffstat (limited to 'challenge-321/roger-bell-west/lua/ch-2.lua')
| -rwxr-xr-x | challenge-321/roger-bell-west/lua/ch-2.lua | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/challenge-321/roger-bell-west/lua/ch-2.lua b/challenge-321/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..60d4d80aa7 --- /dev/null +++ b/challenge-321/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,58 @@ +#! /usr/bin/lua + +function split(t) + local cl = {} + string.gsub(t, + "(.)", + function(c) + table.insert(cl, c) + end + ) + return cl +end + +function join(t) + local out="" + for i, v in ipairs(t) do + out = out .. v + end + return out +end + +function backspacecompare(a, b) + local sa = {} + for _, i in ipairs({a, b}) do + local oa = {} + for _n, c in ipairs(split(i)) do + if c == "#" then + table.remove(oa, #oa) + else + table.insert(oa, c) + end + end + table.insert(sa, join(oa)) + end + return sa[1] == sa[2] +end + +if backspacecompare("ab#c", "ad#c") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if backspacecompare("ab##", "a#b#") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not backspacecompare("a#b", "c") then + io.write("Pass") +else + io.write("FAIL") +end +print("") + |
