aboutsummaryrefslogtreecommitdiff
path: root/challenge-260/roger-bell-west/lua/ch-2.lua
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2024-03-16 20:07:25 -0400
committerGitHub <noreply@github.com>2024-03-16 20:07:25 -0400
commitff5e8ece15a2384fbfb530710f10aa485017d4a5 (patch)
tree6fe268d9ceb2b25a2951ea984c077450feb2efae /challenge-260/roger-bell-west/lua/ch-2.lua
parent60f1003122fbada697317d943c238593f86db579 (diff)
parent62e7fc3bb85a74125663f4fbd0a5911f6f30c81f (diff)
downloadperlweeklychallenge-club-ff5e8ece15a2384fbfb530710f10aa485017d4a5.tar.gz
perlweeklychallenge-club-ff5e8ece15a2384fbfb530710f10aa485017d4a5.tar.bz2
perlweeklychallenge-club-ff5e8ece15a2384fbfb530710f10aa485017d4a5.zip
Merge branch 'manwar:master' into work
Diffstat (limited to 'challenge-260/roger-bell-west/lua/ch-2.lua')
-rwxr-xr-xchallenge-260/roger-bell-west/lua/ch-2.lua102
1 files changed, 102 insertions, 0 deletions
diff --git a/challenge-260/roger-bell-west/lua/ch-2.lua b/challenge-260/roger-bell-west/lua/ch-2.lua
new file mode 100755
index 0000000000..d9407cf3ad
--- /dev/null
+++ b/challenge-260/roger-bell-west/lua/ch-2.lua
@@ -0,0 +1,102 @@
+#! /usr/bin/lua
+
+function deepcopy(src)
+ local dst = {}
+ for k, v in pairs(src) do
+ if type(v) == "table" then
+ v = deepcopy(v)
+ end
+ dst[k] = v
+ end
+ return dst
+end
+
+function permute(a)
+ local out = {}
+ local n = #a
+ local c = {}
+ for i = 1,n do
+ table.insert(c, 1)
+ end
+ table.insert(out, deepcopy(a))
+ local i=1
+ while true do
+ if i > n then
+ break
+ end
+ if c[i] < i then
+ if i % 2 == 1 then
+ a[1],a[i] = a[i],a[1]
+ else
+ a[c[i]],a[i] = a[i],a[c[i]]
+ end
+ table.insert(out, deepcopy(a))
+ c[i] = c[i]+1
+ i = 1
+ else
+ c[i] = 1
+ i = i+1
+ end
+ end
+ return out
+end
+
+function split(t)
+ local cl = {}
+ string.gsub(t,
+ "(.)",
+ function(c)
+ table.insert(cl, c)
+ end
+ )
+ return cl
+end
+
+function join(t)
+ local out=""
+ for k,v in pairs(t) do
+ out = out .. v
+ end
+ return out
+end
+
+function dictionaryrank(a)
+ local c = split(a)
+ local d = {}
+ for _, o in ipairs(permute(c)) do
+ d[join(o)] = true
+ end
+ local dd = {}
+ for k, _ in pairs(d) do
+ table.insert(dd, k)
+ end
+ table.sort(dd)
+ for i, s in ipairs(dd) do
+ if s == a then
+ return i
+ end
+ end
+ return 0
+end
+
+if dictionaryrank("CAT") == 3 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if dictionaryrank("GOOGLE") == 88 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if dictionaryrank("SECRET") == 255 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
+