diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-10-01 07:08:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-01 07:08:29 +0100 |
| commit | 236626df73f6a77ffa3a9ef434b6a05b88896698 (patch) | |
| tree | 5d017ee8c9c01b80c7de982b4cf9983fb40e0067 | |
| parent | 43c5d6e2a1d02048e68f6aaddd874137b140f154 (diff) | |
| parent | 9f221c40fce21616bca718b77baede972affd2a9 (diff) | |
| download | perlweeklychallenge-club-236626df73f6a77ffa3a9ef434b6a05b88896698.tar.gz perlweeklychallenge-club-236626df73f6a77ffa3a9ef434b6a05b88896698.tar.bz2 perlweeklychallenge-club-236626df73f6a77ffa3a9ef434b6a05b88896698.zip | |
Merge pull request #2422 from tylerw/tw/challenge-080-lua
Ch80: add Lua solutions
| -rw-r--r-- | challenge-080/tyler-wardhaugh/lua/README.md | 8 | ||||
| -rwxr-xr-x | challenge-080/tyler-wardhaugh/lua/ch-1.lua | 33 | ||||
| -rwxr-xr-x | challenge-080/tyler-wardhaugh/lua/ch-2.lua | 25 | ||||
| -rwxr-xr-x | challenge-080/tyler-wardhaugh/lua/run.lua | 9 | ||||
| -rwxr-xr-x | challenge-080/tyler-wardhaugh/lua/test.lua | 23 |
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) |
