aboutsummaryrefslogtreecommitdiff
path: root/challenge-307/roger-bell-west/lua/ch-2.lua
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-307/roger-bell-west/lua/ch-2.lua')
-rwxr-xr-xchallenge-307/roger-bell-west/lua/ch-2.lua51
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-307/roger-bell-west/lua/ch-2.lua b/challenge-307/roger-bell-west/lua/ch-2.lua
new file mode 100755
index 0000000000..5e0f75eb2d
--- /dev/null
+++ b/challenge-307/roger-bell-west/lua/ch-2.lua
@@ -0,0 +1,51 @@
+#! /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 findanagrams(a)
+ local b = {}
+ for _, s in ipairs(a) do
+ c = split(s)
+ table.sort(c)
+ table.insert(b, join(c))
+ end
+ local out = 1
+ for i = 2, #a do
+ if b[i - 1] ~= b[i] then
+ out = out + 1
+ end
+ end
+ return out
+end
+
+if findanagrams({"acca", "dog", "god", "perl", "repl"}) == 3 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if findanagrams({"abba", "baba", "aabb", "ab", "ab"}) == 2 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
+