aboutsummaryrefslogtreecommitdiff
path: root/challenge-090/tyler-wardhaugh/lua
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2020-12-09 12:36:23 -0600
committerLuis Mochan <mochan@fis.unam.mx>2020-12-09 12:36:23 -0600
commitda971e8987ceade04a6eecbe59efd44b7e9dffb1 (patch)
tree073bb8527dc39be46412e02e739131f1c613cc7b /challenge-090/tyler-wardhaugh/lua
parentb656bc813129c410b0ccd81449a55222a4f8dcc6 (diff)
parent931e28a9fe63ad0942cf9f3099191a0e21a978c2 (diff)
downloadperlweeklychallenge-club-da971e8987ceade04a6eecbe59efd44b7e9dffb1.tar.gz
perlweeklychallenge-club-da971e8987ceade04a6eecbe59efd44b7e9dffb1.tar.bz2
perlweeklychallenge-club-da971e8987ceade04a6eecbe59efd44b7e9dffb1.zip
Merge branch 'master' of github.com:manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-090/tyler-wardhaugh/lua')
-rw-r--r--challenge-090/tyler-wardhaugh/lua/README.md6
-rwxr-xr-xchallenge-090/tyler-wardhaugh/lua/ch-1.lua35
-rwxr-xr-xchallenge-090/tyler-wardhaugh/lua/ch-2.lua27
-rwxr-xr-xchallenge-090/tyler-wardhaugh/lua/run.lua9
-rwxr-xr-xchallenge-090/tyler-wardhaugh/lua/test.lua21
-rw-r--r--challenge-090/tyler-wardhaugh/lua/util.lua14
6 files changed, 109 insertions, 3 deletions
diff --git a/challenge-090/tyler-wardhaugh/lua/README.md b/challenge-090/tyler-wardhaugh/lua/README.md
index d2f91b15fe..8772dac9c1 100644
--- a/challenge-090/tyler-wardhaugh/lua/README.md
+++ b/challenge-090/tyler-wardhaugh/lua/README.md
@@ -1,17 +1,17 @@
# The Weekly Challenge
-The Weekly Challenge - #089 - Tyler Wardhaugh
+The Weekly Challenge - #090 - Tyler Wardhaugh
## Usage
Run Task 1:
- $ ./run.lua ch-1 N
+ $ ./run.lua ch-1 DNA
Run Task 2:
- $ ./run.lua ch-2 MATRIX-FILE
+ $ ./run.lua ch-2 A B
Run the project's tests (all the samples from the task descriptions plus some others):
diff --git a/challenge-090/tyler-wardhaugh/lua/ch-1.lua b/challenge-090/tyler-wardhaugh/lua/ch-1.lua
new file mode 100755
index 0000000000..e964ce9f6d
--- /dev/null
+++ b/challenge-090/tyler-wardhaugh/lua/ch-1.lua
@@ -0,0 +1,35 @@
+#!/usr/bin/env lua
+
+local t1 = {}
+local util = require'util'
+
+t1.DEFAULT_DNA = 'GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG'
+
+function t1.process_dna(dna)
+ freq = {}
+ for c in dna:gmatch'.' do
+ freq[c] = (freq[c] or 0) + 1
+ end
+
+ local replacement = {A='T', T='A', C='G', G='C'}
+ local complement = dna:gsub('(%w)', function (c) return replacement[c] end)
+
+ return freq, complement
+end
+
+function t1.run(args)
+ local dna = args[1] or t1.DEFAULT_DNA
+ local freq, complement = t1.process_dna(dna)
+
+ io.write('Counts = { ')
+ local seq = util.dict_to_seq(freq)
+ for i, v in ipairs(seq) do
+ io.write(v[1] .. ': ' .. v[2])
+ if i ~= #seq then io.write(', ') end
+ end
+ print(' }\n')
+
+ print('Complement: ' .. complement)
+end
+
+return t1
diff --git a/challenge-090/tyler-wardhaugh/lua/ch-2.lua b/challenge-090/tyler-wardhaugh/lua/ch-2.lua
new file mode 100755
index 0000000000..85c69b7be8
--- /dev/null
+++ b/challenge-090/tyler-wardhaugh/lua/ch-2.lua
@@ -0,0 +1,27 @@
+#!/usr/bin/env lua
+
+local t2 = {}
+
+function t2.ethiopian_multiply(a, b)
+ local product = 0
+
+ while a > 0 do
+ if a % 2 ~= 0 then
+ product = product + b
+ end
+
+ a = a // 2
+ b = b * 2
+ end
+
+ return product
+end
+
+function t2.run(args)
+ local A = tonumber(args[1]) or 12
+ local B = tonumber(args[2]) or 14
+ local product = t2.ethiopian_multiply(A, B)
+ print(product)
+end
+
+return t2
diff --git a/challenge-090/tyler-wardhaugh/lua/run.lua b/challenge-090/tyler-wardhaugh/lua/run.lua
new file mode 100755
index 0000000000..c6e7473bee
--- /dev/null
+++ b/challenge-090/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-090/tyler-wardhaugh/lua/test.lua b/challenge-090/tyler-wardhaugh/lua/test.lua
new file mode 100755
index 0000000000..7dffde2f53
--- /dev/null
+++ b/challenge-090/tyler-wardhaugh/lua/test.lua
@@ -0,0 +1,21 @@
+#!/usr/bin/env lua
+
+require 'busted.runner'()
+
+describe("Task 1, DNA Sequence", function()
+ local t1 = require'ch-1'
+ it("produces correct results for the examples", function()
+ local freq, complement = t1.process_dna(t1.DEFAULT_DNA)
+ assert.are.same({G=13, T=22, A=14, C=18 }, freq)
+ assert.are.same('CATTTGGGGAAAAGTAAATCTGTCTAGCTGAGGAATAGGTAAGAGTCTCTACACAACGACCAGCGGC',
+ complement)
+ end)
+end)
+
+describe("Task 2, Ethiopian Multiplication", function()
+ local t2 = require'ch-2'
+ it("produces correct results for the examples", function()
+ assert.are.same(168, t2.ethiopian_multiply(12, 14))
+ assert.are.same(1554, t2.ethiopian_multiply(37, 42))
+ end)
+end)
diff --git a/challenge-090/tyler-wardhaugh/lua/util.lua b/challenge-090/tyler-wardhaugh/lua/util.lua
new file mode 100644
index 0000000000..1623b04824
--- /dev/null
+++ b/challenge-090/tyler-wardhaugh/lua/util.lua
@@ -0,0 +1,14 @@
+#!/usr/bin/env lua
+
+util = {}
+
+--[[ table ]]--
+function util.dict_to_seq(coll)
+ local result = {}
+ for k, v in pairs(coll) do
+ table.insert(result, { k, v })
+ end
+ return result
+end
+
+return util