aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2021-01-06 08:28:22 -0800
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2021-01-09 16:35:45 -0800
commit37d55e81335dcaadee2f2e0955daa66fa924fe00 (patch)
tree675de1103b3aabde84d43d56ebcac4103587ab12
parent9720d945cd578e71b2334a5032ac36a703e55c9b (diff)
downloadperlweeklychallenge-club-37d55e81335dcaadee2f2e0955daa66fa924fe00.tar.gz
perlweeklychallenge-club-37d55e81335dcaadee2f2e0955daa66fa924fe00.tar.bz2
perlweeklychallenge-club-37d55e81335dcaadee2f2e0955daa66fa924fe00.zip
Ch94 (Lua): Task 1 & Task 2
-rw-r--r--challenge-094/tyler-wardhaugh/lua/README.md4
-rwxr-xr-xchallenge-094/tyler-wardhaugh/lua/ch-1.lua45
-rwxr-xr-xchallenge-094/tyler-wardhaugh/lua/ch-2.lua51
-rwxr-xr-xchallenge-094/tyler-wardhaugh/lua/run.lua9
-rwxr-xr-xchallenge-094/tyler-wardhaugh/lua/test.lua27
-rw-r--r--challenge-094/tyler-wardhaugh/lua/util.lua11
6 files changed, 145 insertions, 2 deletions
diff --git a/challenge-094/tyler-wardhaugh/lua/README.md b/challenge-094/tyler-wardhaugh/lua/README.md
index 2e1e278b45..5e25cb80fd 100644
--- a/challenge-094/tyler-wardhaugh/lua/README.md
+++ b/challenge-094/tyler-wardhaugh/lua/README.md
@@ -7,11 +7,11 @@ The Weekly Challenge - #091 - Tyler Wardhaugh
Run Task 1:
- $ ./run.lua ch-1 N
+ $ ./run.lua ch-1 S1 S2 S3...
Run Task 2:
- $ ./run.lua ch-2 N1 N2 N3...
+ $ ./run.lua ch-2
Run the project's tests (all the samples from the task descriptions plus some others):
diff --git a/challenge-094/tyler-wardhaugh/lua/ch-1.lua b/challenge-094/tyler-wardhaugh/lua/ch-1.lua
new file mode 100755
index 0000000000..36a04bd462
--- /dev/null
+++ b/challenge-094/tyler-wardhaugh/lua/ch-1.lua
@@ -0,0 +1,45 @@
+#!/usr/bin/env lua
+
+require'util'
+
+local t1 = {}
+t1.DEFAULT_INPUT = {"opt", "bat", "saw", "tab", "pot", "top", "was"}
+
+function t1.keygen(s)
+ local tbl = {}
+ s:gsub('.', function(c) table.insert(tbl, c) end)
+ table.sort(tbl)
+ return table.concat(tbl)
+end
+
+function t1.group_anagrams(coll)
+ local groups = util.group_by(t1.keygen, coll)
+
+ -- sorting for stable results (for testing)
+ table.sort(groups)
+
+ return groups
+end
+
+function t1.run(args)
+ local s
+ if #args > 0 then
+ s = args
+ else
+ s = t1.DEFAULT_INPUT
+ end
+ local groups = t1.group_anagrams(s)
+
+ local lead = ''
+ io.write('[ ')
+ for _, v in pairs(groups) do
+ table.sort(v)
+ io.write(lead .. '("')
+ io.write(table.concat(v, '", "'))
+ io.write('")')
+ lead = ' '
+ end
+ print(' ]')
+end
+
+return t1
diff --git a/challenge-094/tyler-wardhaugh/lua/ch-2.lua b/challenge-094/tyler-wardhaugh/lua/ch-2.lua
new file mode 100755
index 0000000000..37fc925e3d
--- /dev/null
+++ b/challenge-094/tyler-wardhaugh/lua/ch-2.lua
@@ -0,0 +1,51 @@
+#!/usr/bin/env lua
+
+local t2 = {}
+
+--[[
+ 1
+ / \
+ 2 3
+ / \
+ 4 5
+ / \
+ 6 7
+--]]
+t2.DEFAULT_INPUT = {1, {2, {4}, {5, {6}, {7}}}, {3}}
+
+function t2.tree_to_linked_list(tree)
+ local function _tree_to_linked_list(tree, results)
+ local val = tree[1]
+ local left = tree[2]
+ local right = tree[3]
+
+ if val then
+ table.insert(results, val)
+ end
+
+ if left then
+ _tree_to_linked_list(left, results)
+ end
+
+ if right then
+ _tree_to_linked_list(right, results)
+ end
+
+ return results
+ end
+ return _tree_to_linked_list(tree, {})
+end
+
+function t2.run(args)
+ local tree
+ if #args > 0 then
+ tree = args
+ else
+ tree = t2.DEFAULT_INPUT
+ end
+
+ local result = t2.tree_to_linked_list(tree)
+ print(table.concat(result, ' -> '))
+end
+
+return t2
diff --git a/challenge-094/tyler-wardhaugh/lua/run.lua b/challenge-094/tyler-wardhaugh/lua/run.lua
new file mode 100755
index 0000000000..c6e7473bee
--- /dev/null
+++ b/challenge-094/tyler-wardhaugh/lua/run.lua
@@ -0,0 +1,9 @@
+#!/usr/bin/env lua
+
+local filename = arg[1]
+local run_args = table.move(arg, 2, #arg, 1, {})
+
+io.write(string.format("Running task from '%s' with {%s}:\n",
+ filename, table.concat(run_args, ", ")))
+
+require(filename).run(run_args)
diff --git a/challenge-094/tyler-wardhaugh/lua/test.lua b/challenge-094/tyler-wardhaugh/lua/test.lua
new file mode 100755
index 0000000000..68a7815ded
--- /dev/null
+++ b/challenge-094/tyler-wardhaugh/lua/test.lua
@@ -0,0 +1,27 @@
+#!/usr/bin/env lua
+
+require 'busted.runner'()
+
+describe("Task 1, Group Anagrams", function()
+ local t1 = require'ch-1'
+ it("produces correct results for the examples", function()
+ assert.are.same(
+ {
+ abt={'bat', 'tab'},
+ asw={'saw', 'was'},
+ opt={'opt', 'pot', 'top'}
+ },
+ t1.group_anagrams({"opt", "bat", "saw", "tab", "pot", "top", "was"}))
+ assert.are.same({x={'x'}}, t1.group_anagrams({"x"}))
+ end)
+end)
+
+describe("Task 2, Binary Tree to Linked List", function()
+ local t2 = require'ch-2'
+ it("produces correct results for the examples", function()
+ assert.are.same(
+ { 1, 2, 4, 5, 6, 7, 3 },
+ t2.tree_to_linked_list({1, {2, {4}, {5, {6}, {7}}}, {3}})
+ )
+ end)
+end)
diff --git a/challenge-094/tyler-wardhaugh/lua/util.lua b/challenge-094/tyler-wardhaugh/lua/util.lua
new file mode 100644
index 0000000000..855b5d34fb
--- /dev/null
+++ b/challenge-094/tyler-wardhaugh/lua/util.lua
@@ -0,0 +1,11 @@
+util = {}
+
+function util.group_by(f, coll)
+ local result = {}
+ for _, v in pairs(coll) do
+ local k = f(v)
+ if not result[k] then result[k] = {} end
+ table.insert(result[k], v)
+ end
+ return result
+end