aboutsummaryrefslogtreecommitdiff
path: root/challenge-321/roger-bell-west/lua/ch-2.lua
diff options
context:
space:
mode:
authorRoger Bell_West <roger@firedrake.org>2025-05-13 14:06:50 +0100
committerRoger Bell_West <roger@firedrake.org>2025-05-13 14:06:50 +0100
commitaa6afc31ada526c649d8490e9bdcb416027686f2 (patch)
tree8a6a19b64ac7a3463e4e0b99d62cc2d7053b7b57 /challenge-321/roger-bell-west/lua/ch-2.lua
parent152c0ffaaa3df8dfa2a5dbff97971ec378999b0a (diff)
downloadperlweeklychallenge-club-aa6afc31ada526c649d8490e9bdcb416027686f2.tar.gz
perlweeklychallenge-club-aa6afc31ada526c649d8490e9bdcb416027686f2.tar.bz2
perlweeklychallenge-club-aa6afc31ada526c649d8490e9bdcb416027686f2.zip
RogerBW solutions for challenge no. 321
Diffstat (limited to 'challenge-321/roger-bell-west/lua/ch-2.lua')
-rwxr-xr-xchallenge-321/roger-bell-west/lua/ch-2.lua58
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("")
+