diff options
Diffstat (limited to 'challenge-098/tyler-wardhaugh/lua')
| -rw-r--r-- | challenge-098/tyler-wardhaugh/lua/README.md | 8 | ||||
| -rwxr-xr-x | challenge-098/tyler-wardhaugh/lua/ch-1.lua | 31 | ||||
| -rwxr-xr-x | challenge-098/tyler-wardhaugh/lua/ch-2.lua | 40 | ||||
| -rwxr-xr-x | challenge-098/tyler-wardhaugh/lua/run.lua | 9 | ||||
| -rwxr-xr-x | challenge-098/tyler-wardhaugh/lua/test.lua | 24 |
5 files changed, 108 insertions, 4 deletions
diff --git a/challenge-098/tyler-wardhaugh/lua/README.md b/challenge-098/tyler-wardhaugh/lua/README.md index 27612d81d8..b653496267 100644 --- a/challenge-098/tyler-wardhaugh/lua/README.md +++ b/challenge-098/tyler-wardhaugh/lua/README.md @@ -1,17 +1,17 @@ # The Weekly Challenge -The Weekly Challenge - #097 - Tyler Wardhaugh +The Weekly Challenge - #098 - Tyler Wardhaugh ## Usage -Run Task 1: +Run Task 1 with input: - $ ./run.lua ch-1 S N + $ ./run.lua ch-1 FILE N Run Task 2: - $ ./run.lua ch-2 B S + $ ./run.lua ch-2 Run the project's tests (all the samples from the task descriptions plus some others): diff --git a/challenge-098/tyler-wardhaugh/lua/ch-1.lua b/challenge-098/tyler-wardhaugh/lua/ch-1.lua new file mode 100755 index 0000000000..f3f28177af --- /dev/null +++ b/challenge-098/tyler-wardhaugh/lua/ch-1.lua @@ -0,0 +1,31 @@ +#!/usr/bin/env lua + +local t1 = {} +t1.DEFAULT_INPUT = {'../clojure/resources/input.txt', 4} +t1.FILES = {} + +function t1.readN(filename, n) + local fp = t1.FILES[filename] or assert(io.open(filename, "r")) + local result = fp:read(n) + t1.FILES[filename] = fp + return result +end + +function t1.run(args) + local filename, n + if #args > 0 then + filename, n = args[1], tonumber(args[2]) + else + filename, n = table.unpack(t1.DEFAULT_INPUT) + end + + local result + for _=1,3 do + result = t1.readN(filename, n) + if result ~= nil then + print(result) + end + end +end + +return t1 diff --git a/challenge-098/tyler-wardhaugh/lua/ch-2.lua b/challenge-098/tyler-wardhaugh/lua/ch-2.lua new file mode 100755 index 0000000000..0a182da10c --- /dev/null +++ b/challenge-098/tyler-wardhaugh/lua/ch-2.lua @@ -0,0 +1,40 @@ +local t2 = {} +t2.DEFAULT_INPUT = {{1, 2, 3, 4}, 3} + +function t2.binary_search(coll, n, startp, endp) + if endp >= startp then + local mid = startp + (endp - startp) // 2 + local v = coll[mid] + + if v == n then + return mid + elseif v > n then + return t2.binary_search(coll, n, startp, mid - 1) + else + return t2.binary_search(coll, n, mid + 1, endp) + end + else + return -1 * startp - 1 + end +end + +function t2.search_insert_position(coll, n) + local index = t2.binary_search(coll, n, 1, #coll) + -- Lua's tables are 1-indexed, hence we need to account for that in both + -- cases, both when the result is negative (meaning the number isn't in the + -- list, and when the result is positive, indicating the number was found. + if index < 0 then + return -1 * index - 2 + else + return index - 1 + end +end + +function t2.run(_) + local ns, n + ns, n = table.unpack(t2.DEFAULT_INPUT) + + print(t2.search_insert_position(ns, n)) +end + +return t2 diff --git a/challenge-098/tyler-wardhaugh/lua/run.lua b/challenge-098/tyler-wardhaugh/lua/run.lua new file mode 100755 index 0000000000..c6e7473bee --- /dev/null +++ b/challenge-098/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-098/tyler-wardhaugh/lua/test.lua b/challenge-098/tyler-wardhaugh/lua/test.lua new file mode 100755 index 0000000000..9247faa11e --- /dev/null +++ b/challenge-098/tyler-wardhaugh/lua/test.lua @@ -0,0 +1,24 @@ +#!/usr/bin/env lua + +require 'busted.runner'() + +describe("Task 1, Read N-Characters", function() + local t1 = require'ch-1' + local input_filename = '../clojure/resources/input.txt' + it("produces correct results for the examples", function() + assert.are.same("1234", t1.readN(input_filename, 4)) + assert.are.same("5678", t1.readN(input_filename, 4)) + assert.are.same("90", t1.readN(input_filename, 4)) + end) +end) + + +describe("Task 2, Search Insert Position", function() + local t2 = require'ch-2' + it("produces correct results for the examples", function() + assert.are.same(2, t2.search_insert_position({1, 2, 3, 4}, 3)) + assert.are.same(3, t2.search_insert_position({1, 3, 5, 7}, 6)) + assert.are.same(0, t2.search_insert_position({12, 14, 16, 18}, 10)) + assert.are.same(4, t2.search_insert_position({11, 13, 15, 17}, 19)) + end) +end) |
