aboutsummaryrefslogtreecommitdiff
path: root/challenge-145/roger-bell-west/lua/ch-2.lua
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-12-28 10:05:40 +0000
committerGitHub <noreply@github.com>2021-12-28 10:05:40 +0000
commit844ebffd0204578147175bcbc809d336a90bb9c2 (patch)
tree4cf5bcbe97f15360f7ab43c6d1e80494aebf8f7c /challenge-145/roger-bell-west/lua/ch-2.lua
parentaae1ef67142f7503a3a8996ab70f545952388995 (diff)
parent622f36b68f9993ec54f54ca85c2939719f06d8cb (diff)
downloadperlweeklychallenge-club-844ebffd0204578147175bcbc809d336a90bb9c2.tar.gz
perlweeklychallenge-club-844ebffd0204578147175bcbc809d336a90bb9c2.tar.bz2
perlweeklychallenge-club-844ebffd0204578147175bcbc809d336a90bb9c2.zip
Merge pull request #5429 from Firedrake/rogerbw-challenge-145
Solutions for challenge #145
Diffstat (limited to 'challenge-145/roger-bell-west/lua/ch-2.lua')
-rwxr-xr-xchallenge-145/roger-bell-west/lua/ch-2.lua87
1 files changed, 87 insertions, 0 deletions
diff --git a/challenge-145/roger-bell-west/lua/ch-2.lua b/challenge-145/roger-bell-west/lua/ch-2.lua
new file mode 100755
index 0000000000..f12b3797a2
--- /dev/null
+++ b/challenge-145/roger-bell-west/lua/ch-2.lua
@@ -0,0 +1,87 @@
+#! /usr/bin/lua
+
+-- by Michael Anderson at
+-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua
+function recursive_compare(t1,t2)
+ -- Use usual comparison first.
+ if t1==t2 then return true end
+ -- We only support non-default behavior for tables
+ if (type(t1)~="table") then return false end
+ -- They better have the same metatables
+ local mt1 = getmetatable(t1)
+ local mt2 = getmetatable(t2)
+ if( not recursive_compare(mt1,mt2) ) then return false end
+
+ -- Check each key-value pair
+ -- We have to do this both ways in case we miss some.
+ -- TODO: Could probably be smarter and not check those we've
+ -- already checked though!
+ for k1,v1 in pairs(t1) do
+ local v2 = t2[k1]
+ if( not recursive_compare(v1,v2) ) then return false end
+ end
+ for k2,v2 in pairs(t2) do
+ local v1 = t1[k2]
+ if( not recursive_compare(v1,v2) ) then return false end
+ end
+
+ return true
+end
+
+function eertree(str)
+ pal={}
+ q={}
+ for i = 1,#str do
+ for j = i,#str do
+ strpal=string.sub(str,i,j)
+ strrev=string.reverse(strpal)
+ if strpal == strrev and q[strpal] == nil then
+ table.insert(pal,strpal)
+ q[strpal]=true
+ end
+ end
+ end
+ return pal
+end
+
+if recursive_compare(eertree("redivider"),{"r","redivider","e","edivide","d","divid","i","ivi","v"}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if recursive_compare(eertree("deific"),{"d","e","i","ifi","f","c"}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if recursive_compare(eertree("rotors"),{"r","rotor","o","oto","t","s"}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if recursive_compare(eertree("challenge"),{"c","h","a","l","ll","e","n","g"}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if recursive_compare(eertree("champion"),{"c","h","a","m","p","i","o","n"}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if recursive_compare(eertree("christmas"),{"c","h","r","i","s","t","m","a"}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")