diff options
| author | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2020-12-08 13:07:11 -0800 |
|---|---|---|
| committer | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2020-12-08 13:18:31 -0800 |
| commit | ec53034d57fe6d3a652c29ad0f6b6ba33d8ed848 (patch) | |
| tree | a0fb2875a7a39dbdc26be557075748b19596b900 | |
| parent | 085764e526d26c50a09a2c0c18da809268e32f33 (diff) | |
| download | perlweeklychallenge-club-ec53034d57fe6d3a652c29ad0f6b6ba33d8ed848.tar.gz perlweeklychallenge-club-ec53034d57fe6d3a652c29ad0f6b6ba33d8ed848.tar.bz2 perlweeklychallenge-club-ec53034d57fe6d3a652c29ad0f6b6ba33d8ed848.zip | |
Ch90 (Lua): Tasks 1 & 2
| -rwxr-xr-x | challenge-090/tyler-wardhaugh/lua/ch-1.lua | 35 | ||||
| -rwxr-xr-x | challenge-090/tyler-wardhaugh/lua/ch-2.lua | 27 | ||||
| -rwxr-xr-x | challenge-090/tyler-wardhaugh/lua/run.lua | 9 | ||||
| -rwxr-xr-x | challenge-090/tyler-wardhaugh/lua/test.lua | 21 | ||||
| -rw-r--r-- | challenge-090/tyler-wardhaugh/lua/util.lua | 14 |
5 files changed, 106 insertions, 0 deletions
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 |
