From 508b49f4cef1ec360e5a9ec738c46f60a92c3c0b Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 25 Jun 2024 14:24:54 +0100 Subject: RogerBW solutions for challenge no. 275 --- challenge-275/roger-bell-west/lua/ch-2.lua | 63 ++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 challenge-275/roger-bell-west/lua/ch-2.lua (limited to 'challenge-275/roger-bell-west/lua/ch-2.lua') 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("") + -- cgit