aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-078/tyler-wardhaugh/clojure/test/tw/weekly/c78_test.clj4
-rw-r--r--challenge-078/tyler-wardhaugh/lua/README.md23
-rwxr-xr-xchallenge-078/tyler-wardhaugh/lua/ch-1.lua21
-rwxr-xr-xchallenge-078/tyler-wardhaugh/lua/ch-2.lua22
-rwxr-xr-xchallenge-078/tyler-wardhaugh/lua/test.lua25
5 files changed, 93 insertions, 2 deletions
diff --git a/challenge-078/tyler-wardhaugh/clojure/test/tw/weekly/c78_test.clj b/challenge-078/tyler-wardhaugh/clojure/test/tw/weekly/c78_test.clj
index d4de3f4eac..d50c9e164f 100644
--- a/challenge-078/tyler-wardhaugh/clojure/test/tw/weekly/c78_test.clj
+++ b/challenge-078/tyler-wardhaugh/clojure/test/tw/weekly/c78_test.clj
@@ -5,8 +5,8 @@
(deftest task-1
(testing "Task 1, Leader Elements"
- (is (= (leader-elements [9, 10, 7, 5, 6, 1]) (list 10 7 6 1))
- (is (= (leader-elements [3 4 5]) (list 5))))))
+ (is (= (leader-elements [9, 10, 7, 5, 6, 1]) (list 10 7 6 1)))
+ (is (= (leader-elements [3 4 5]) (list 5)))))
(deftest task-2
(testing "Task 2, Left Rotation"
diff --git a/challenge-078/tyler-wardhaugh/lua/README.md b/challenge-078/tyler-wardhaugh/lua/README.md
new file mode 100644
index 0000000000..91e2f8575d
--- /dev/null
+++ b/challenge-078/tyler-wardhaugh/lua/README.md
@@ -0,0 +1,23 @@
+
+# The Weekly Challenge
+
+The Weekly Challenge - #078 - Tyler Wardhaugh
+
+## Usage
+
+Run Task 1:
+
+ $ lua -e 'require"ch-1".run({9, 10, 7, 5, 6, 1})'
+
+Run Task 2:
+
+ $ lua -e 'require"ch-2".run({10, 20, 30, 40, 50}, {3, 4})'
+
+Run the project's tests (which are samples from the task descriptions):
+
+ $ lua test.lua
+
+## Requirements:
+* [Lua](https://www.lua.org/) 5.3
+* [LuaRocks](https://luarocks.org/)
+* [busted](https://olivinelabs.com/busted/) (unit testing framework)
diff --git a/challenge-078/tyler-wardhaugh/lua/ch-1.lua b/challenge-078/tyler-wardhaugh/lua/ch-1.lua
new file mode 100755
index 0000000000..39c408e62c
--- /dev/null
+++ b/challenge-078/tyler-wardhaugh/lua/ch-1.lua
@@ -0,0 +1,21 @@
+#!/usr/bin/env lua
+
+local t1 = {}
+
+function t1.leaders(a)
+ local ldrs = {-1}
+ for i=#a, 1, -1 do
+ if a[i] > ldrs[1] then
+ table.insert(ldrs, 1, a[i])
+ end
+ end
+ table.remove(ldrs, #ldrs)
+ return ldrs
+end
+
+function t1.run(a)
+ local res = table.concat(t1.leaders(a), ', ')
+ print('(' .. res .. ')')
+end
+
+return t1
diff --git a/challenge-078/tyler-wardhaugh/lua/ch-2.lua b/challenge-078/tyler-wardhaugh/lua/ch-2.lua
new file mode 100755
index 0000000000..f5b2ab901a
--- /dev/null
+++ b/challenge-078/tyler-wardhaugh/lua/ch-2.lua
@@ -0,0 +1,22 @@
+#!/usr/bin/env lua
+
+local t2 = {}
+
+function t2.multi_rotate(list, rotations)
+ local rts = {}
+ for _, rv in ipairs(rotations) do
+ local rotated = table.move(list, rv + 1, #list, 1, {})
+ for i = 1, rv do table.insert(rotated, list[i]) end
+ table.insert(rts, rotated)
+ end
+ return rts
+end
+
+function t2.run(...)
+ local res = t2.multi_rotate(...)
+ for _, v in ipairs(res) do
+ print('[' .. table.concat(v, ' ') .. ']')
+ end
+end
+
+return t2
diff --git a/challenge-078/tyler-wardhaugh/lua/test.lua b/challenge-078/tyler-wardhaugh/lua/test.lua
new file mode 100755
index 0000000000..c5494095c0
--- /dev/null
+++ b/challenge-078/tyler-wardhaugh/lua/test.lua
@@ -0,0 +1,25 @@
+#!/usr/bin/env lua
+
+require 'busted.runner'()
+
+describe("Task 1, Leader Elements", function()
+ local t1 = require'ch-1'
+ it("produces correct results for the examples", function()
+ assert.are.same({10, 7, 6, 1}, t1.leaders({9, 10, 7, 5, 6, 1}))
+ assert.are.same({5}, t1.leaders({3, 4, 5}))
+ end)
+end)
+
+describe("Task 2, Left Rotation", function()
+ local t2 = require'ch-2'
+ it("produces correct results for the examples", function()
+ assert.are.same(
+ {{40, 50, 10, 20, 30}, {50, 10, 20, 30, 40}},
+ t2.multi_rotate({10, 20, 30, 40, 50}, {3, 4})
+ )
+ assert.are.same(
+ {{4, 2, 6, 3, 7}, {6, 3, 7, 4, 2}, {3, 7, 4, 2, 6}},
+ t2.multi_rotate({7, 4, 2, 6, 3}, {1, 3, 4})
+ )
+ end)
+end)