aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-080/tyler-wardhaugh/lua/README.md8
-rwxr-xr-xchallenge-080/tyler-wardhaugh/lua/ch-1.lua33
-rwxr-xr-xchallenge-080/tyler-wardhaugh/lua/ch-2.lua25
-rwxr-xr-xchallenge-080/tyler-wardhaugh/lua/run.lua9
-rwxr-xr-xchallenge-080/tyler-wardhaugh/lua/test.lua23
5 files changed, 94 insertions, 4 deletions
diff --git a/challenge-080/tyler-wardhaugh/lua/README.md b/challenge-080/tyler-wardhaugh/lua/README.md
index db3ed46069..c469977934 100644
--- a/challenge-080/tyler-wardhaugh/lua/README.md
+++ b/challenge-080/tyler-wardhaugh/lua/README.md
@@ -7,15 +7,15 @@ The Weekly Challenge - #080 - Tyler Wardhaugh
Run Task 1:
- $
+ $ ./run.lua ch-1 5 2 -2 0
Run Task 2:
- $
+ $ ./run.lua ch-2 1 2 2
-Run the project's tests (which are samples from the task descriptions):
+Run the project's tests (all the samples from the task descriptions plus some others):
- $ lua test.lua
+ $ ./test.lua
## Requirements:
* [Lua](https://www.lua.org/) 5.3
diff --git a/challenge-080/tyler-wardhaugh/lua/ch-1.lua b/challenge-080/tyler-wardhaugh/lua/ch-1.lua
new file mode 100755
index 0000000000..aa54879fac
--- /dev/null
+++ b/challenge-080/tyler-wardhaugh/lua/ch-1.lua
@@ -0,0 +1,33 @@
+#!/usr/bin/env lua
+
+local t1 = {}
+
+function t1.smallest_missing(coll)
+ local range = {0}
+ for _, v in ipairs(coll) do
+ if v > 0 then table.insert(range, v) end
+ end
+ table.sort(range)
+
+ local next, cur
+ for i=1,#range do
+ cur = range[i]
+ next = range[i+1]
+
+ if next and ((next - cur) > 1) then
+ return cur + 1
+ end
+ end
+
+ -- there are no gaps, therefore the smallest "missing" positive integer is
+ -- the maximum number given (i.e., the last one in the sorted table) plus one
+ return range[#range] + 1
+end
+
+function t1.run(arg)
+ local N = {}
+ for _, v in ipairs(arg) do table.insert(N, tonumber(v)) end
+ print(t1.smallest_missing(N))
+end
+
+return t1
diff --git a/challenge-080/tyler-wardhaugh/lua/ch-2.lua b/challenge-080/tyler-wardhaugh/lua/ch-2.lua
new file mode 100755
index 0000000000..54d355d037
--- /dev/null
+++ b/challenge-080/tyler-wardhaugh/lua/ch-2.lua
@@ -0,0 +1,25 @@
+#!/usr/bin/env lua
+
+local t2 = {}
+
+function t2.count_candies(coll)
+ local count = #coll
+ local maybe_inc = function(i, j)
+ if (coll[i] > coll[j]) then count = count + 1 end
+ end
+
+ -- sweep left-to-right
+ for i=1,#coll-1 do maybe_inc(i, i+1) end
+ -- sweep right-to-left
+ for i=#coll,2,-1 do maybe_inc(i, i-1) end
+
+ return count
+end
+
+function t2.run(arg)
+ local N = {}
+ for _, v in ipairs(arg) do table.insert(N, tonumber(v)) end
+ print(t2.count_candies(N))
+end
+
+return t2
diff --git a/challenge-080/tyler-wardhaugh/lua/run.lua b/challenge-080/tyler-wardhaugh/lua/run.lua
new file mode 100755
index 0000000000..c6e7473bee
--- /dev/null
+++ b/challenge-080/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-080/tyler-wardhaugh/lua/test.lua b/challenge-080/tyler-wardhaugh/lua/test.lua
new file mode 100755
index 0000000000..44d03f8844
--- /dev/null
+++ b/challenge-080/tyler-wardhaugh/lua/test.lua
@@ -0,0 +1,23 @@
+#!/usr/bin/env lua
+
+require 'busted.runner'()
+
+describe("Task 1, Smallest Positive Number", function()
+ local t1 = require'ch-1'
+ it("produces correct results for the examples", function()
+ assert.are.same(t1.smallest_missing({5, 2, -2, 0}), 1)
+ assert.are.same(t1.smallest_missing({1, 8, -1}), 2)
+ assert.are.same(t1.smallest_missing({2, 0, -1}), 1)
+ assert.are.same(t1.smallest_missing({-5, -4, -3}), 1)
+ assert.are.same(t1.smallest_missing({1, 2, 3, 10000000}), 4)
+ assert.are.same(t1.smallest_missing({1, 4, 2, 3}), 5)
+ end)
+end)
+
+describe("Task 2, Count Candies", function()
+ local t2 = require'ch-2'
+ it("produces correct results for the examples", function()
+ assert.are.same(t2.count_candies({1, 2, 2}), 4)
+ assert.are.same(t2.count_candies({1, 4, 3, 2}), 7)
+ end)
+end)