aboutsummaryrefslogtreecommitdiff
path: root/challenge-275/roger-bell-west/lua/ch-2.lua
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-06-25 22:27:26 +0100
committerGitHub <noreply@github.com>2024-06-25 22:27:26 +0100
commit9a8f0b8043c806ea406fdaddc718ccd263fbc093 (patch)
tree6084b8d9d56b7370d12c2de83e952ae23bb4eb90 /challenge-275/roger-bell-west/lua/ch-2.lua
parent815048f1183179a10ebb61c46e2c4160925b837b (diff)
parent508b49f4cef1ec360e5a9ec738c46f60a92c3c0b (diff)
downloadperlweeklychallenge-club-9a8f0b8043c806ea406fdaddc718ccd263fbc093.tar.gz
perlweeklychallenge-club-9a8f0b8043c806ea406fdaddc718ccd263fbc093.tar.bz2
perlweeklychallenge-club-9a8f0b8043c806ea406fdaddc718ccd263fbc093.zip
Merge pull request #10325 from Firedrake/rogerbw-challenge-275
RogerBW solutions for challenge no. 275
Diffstat (limited to 'challenge-275/roger-bell-west/lua/ch-2.lua')
-rwxr-xr-xchallenge-275/roger-bell-west/lua/ch-2.lua63
1 files changed, 63 insertions, 0 deletions
diff --git a/challenge-275/roger-bell-west/lua/ch-2.lua b/challenge-275/roger-bell-west/lua/ch-2.lua
new file mode 100755
index 0000000000..021d1aa368
--- /dev/null
+++ b/challenge-275/roger-bell-west/lua/ch-2.lua
@@ -0,0 +1,63 @@
+#! /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 replacedigits(a)
+ local out = {}
+ local prev = 0
+ for _, c in ipairs(split(a)) do
+ if c >= "0" and c <= "9" then
+ table.insert(out, string.char(prev + c))
+ else
+ prev = string.byte(c)
+ table.insert(out, c)
+ end
+ end
+ return join(out)
+end
+
+if replacedigits("a1c1e1") == "abcdef" then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if replacedigits("a1b2c3d4") == "abbdcfdh" then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if replacedigits("b2b") == "bdb" then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if replacedigits("a16z") == "abgz" then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
+