aboutsummaryrefslogtreecommitdiff
path: root/challenge-094
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-01-10 07:46:21 +0000
committerGitHub <noreply@github.com>2021-01-10 07:46:21 +0000
commit098fbee64b9eae2ff8e7137e4861576972190958 (patch)
tree39177c492b62143327e739af71cad8e3533fbea6 /challenge-094
parentbc2d9def1a2b0d6c5501434c2ea2535952ccda15 (diff)
parent37d55e81335dcaadee2f2e0955daa66fa924fe00 (diff)
downloadperlweeklychallenge-club-098fbee64b9eae2ff8e7137e4861576972190958.tar.gz
perlweeklychallenge-club-098fbee64b9eae2ff8e7137e4861576972190958.tar.bz2
perlweeklychallenge-club-098fbee64b9eae2ff8e7137e4861576972190958.zip
Merge pull request #3195 from tylerw/tw/challenge-094
Challenge 094 in Lua
Diffstat (limited to 'challenge-094')
-rw-r--r--challenge-094/tyler-wardhaugh/clojure/test/tw/weekly/c94_test.clj2
-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
7 files changed, 146 insertions, 3 deletions
diff --git a/challenge-094/tyler-wardhaugh/clojure/test/tw/weekly/c94_test.clj b/challenge-094/tyler-wardhaugh/clojure/test/tw/weekly/c94_test.clj
index 4aa5dde415..2acf95e4bd 100644
--- a/challenge-094/tyler-wardhaugh/clojure/test/tw/weekly/c94_test.clj
+++ b/challenge-094/tyler-wardhaugh/clojure/test/tw/weekly/c94_test.clj
@@ -4,7 +4,7 @@
[tw.weekly.c94.t2 :refer [flatten-tree]]))
(deftest task-1
- (testing "Task 1, Max Points"
+ (testing "Task 1, Group Anagrams"
(is (= #{ #{"bat", "tab"},
#{"saw", "was"},
#{"top", "pot", "opt"}}
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