diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-25 22:27:26 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-25 22:27:26 +0100 |
| commit | 9a8f0b8043c806ea406fdaddc718ccd263fbc093 (patch) | |
| tree | 6084b8d9d56b7370d12c2de83e952ae23bb4eb90 /challenge-275/roger-bell-west/lua | |
| parent | 815048f1183179a10ebb61c46e2c4160925b837b (diff) | |
| parent | 508b49f4cef1ec360e5a9ec738c46f60a92c3c0b (diff) | |
| download | perlweeklychallenge-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')
| -rwxr-xr-x | challenge-275/roger-bell-west/lua/ch-1.lua | 70 | ||||
| -rwxr-xr-x | challenge-275/roger-bell-west/lua/ch-2.lua | 63 |
2 files changed, 133 insertions, 0 deletions
diff --git a/challenge-275/roger-bell-west/lua/ch-1.lua b/challenge-275/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..456f420de6 --- /dev/null +++ b/challenge-275/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,70 @@ +#! /usr/bin/lua + +-- bart at https://stackoverflow.com/questions/1426954/split-string-in-lua +function splits(inputstr, sep) + sep=sep or '%s' + local t={} + for field,s in string.gmatch(inputstr, "([^"..sep.."]*)("..sep.."?)") do + table.insert(t,field) + if s=="" then + return t + end + end +end + +function split(t) + local cl = {} + string.gsub(t, + "(.)", + function(c) + table.insert(cl, c) + end + ) + return cl +end + +function brokenkeys(a, k) + local out = 0 + local failset = {} + for _, c in ipairs(k) do + failset[string.lower(c)] = true + end + for _, word in ipairs(splits(string.lower(a), " ")) do + local wordset = {} + for _b, c in ipairs(split(word)) do + wordset[c] = true + end + local intersect = {} + for k, v in pairs(failset) do + if wordset[k] ~= nil then + table.insert(intersect, k) + end + end + if #intersect == 0 then + out = out + 1 + end + end + return out +end + +if brokenkeys("Perl Weekly Challenge", {"l", "a"}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if brokenkeys("Perl and Raku", {"a"}) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if brokenkeys("Well done Team PWC", {"l", "o"}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + 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("") + |
