diff options
| author | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2021-01-20 12:42:56 -0800 |
|---|---|---|
| committer | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2021-01-20 13:21:03 -0800 |
| commit | d803b8295cb052657601c0f90392afac7b5fde47 (patch) | |
| tree | b69271d7765acf83b75d774a3b1a50da0a65ca51 | |
| parent | e788f447e46760cc68c99a605e1d0b0820184304 (diff) | |
| download | perlweeklychallenge-club-d803b8295cb052657601c0f90392afac7b5fde47.tar.gz perlweeklychallenge-club-d803b8295cb052657601c0f90392afac7b5fde47.tar.bz2 perlweeklychallenge-club-d803b8295cb052657601c0f90392afac7b5fde47.zip | |
Ch96 (Lua): Tasks 1 & 2
| -rw-r--r-- | challenge-096/tyler-wardhaugh/lua/README.md | 6 | ||||
| -rwxr-xr-x | challenge-096/tyler-wardhaugh/lua/ch-1.lua | 25 | ||||
| -rwxr-xr-x | challenge-096/tyler-wardhaugh/lua/ch-2.lua | 50 | ||||
| -rwxr-xr-x | challenge-096/tyler-wardhaugh/lua/run.lua | 9 | ||||
| -rwxr-xr-x | challenge-096/tyler-wardhaugh/lua/test.lua | 21 |
5 files changed, 108 insertions, 3 deletions
diff --git a/challenge-096/tyler-wardhaugh/lua/README.md b/challenge-096/tyler-wardhaugh/lua/README.md index 4898f791cd..b916c3c458 100644 --- a/challenge-096/tyler-wardhaugh/lua/README.md +++ b/challenge-096/tyler-wardhaugh/lua/README.md @@ -1,17 +1,17 @@ # The Weekly Challenge -The Weekly Challenge - #095 - Tyler Wardhaugh +The Weekly Challenge - #096 - Tyler Wardhaugh ## Usage Run Task 1: - $ ./run.lua ch-1 N + $ ./run.lua ch-1 S Run Task 2: - $ ./run.lua ch-2 + $ ./run.lua ch-2 S1 S2 Run the project's tests (all the samples from the task descriptions plus some others): diff --git a/challenge-096/tyler-wardhaugh/lua/ch-1.lua b/challenge-096/tyler-wardhaugh/lua/ch-1.lua new file mode 100755 index 0000000000..6538be9718 --- /dev/null +++ b/challenge-096/tyler-wardhaugh/lua/ch-1.lua @@ -0,0 +1,25 @@ +#!/usr/bin/env lua + +local t1 = {} +t1.DEFAULT_INPUT = 'The Weekly Challenge' + +function t1.reverse_words(s) + local words = {} + for word in s:gmatch('%S+') do + table.insert(words, 1, word) + end + return table.concat(words, ' ') +end + +function t1.run(args) + local s + if #args > 0 then + s = args[1] + else + s = t1.DEFAULT_INPUT + end + + print(t1.reverse_words(s)) +end + +return t1 diff --git a/challenge-096/tyler-wardhaugh/lua/ch-2.lua b/challenge-096/tyler-wardhaugh/lua/ch-2.lua new file mode 100755 index 0000000000..c94022f48c --- /dev/null +++ b/challenge-096/tyler-wardhaugh/lua/ch-2.lua @@ -0,0 +1,50 @@ +local t2 = {} +t2.DEFAULT_INPUT = {'kitten', 'sitting'} + +function t2.zeros_matrix(m, n) + local matrix = {} + for i = 1, m do + matrix[i] = {} + for j = 1, n do + matrix[i][j] = 0 + end + end + return matrix + end + +function t2.edit_distance(s1, s2) + local m = #s1 + local n = #s2 + local dp = t2.zeros_matrix(m, n) + + for i = 1, m do + for j = 1, n do + if i == 1 then + dp[i][j] = j + elseif j == 1 then + dp[i][j] = i + elseif s1:sub(i, i) == s2:sub(j, j) then + dp[i][j] = dp[i - 1][j - 1] + else + dp[i][j] = 1 + math.min(dp[i][j - 1], + dp[i - 1][j], + dp[i - 1][j - 1]) + end + end + end + + return dp[m][n] +end + +function t2.run(args) + local s1, s2 + if #args > 0 then + s1, s2 = args[1], args[2] + else + s1, s2 = table.unpack(t2.DEFAULT_INPUT) + end + + print(t2.edit_distance(s1, s2)) +end + +return t2 diff --git a/challenge-096/tyler-wardhaugh/lua/run.lua b/challenge-096/tyler-wardhaugh/lua/run.lua new file mode 100755 index 0000000000..c6e7473bee --- /dev/null +++ b/challenge-096/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-096/tyler-wardhaugh/lua/test.lua b/challenge-096/tyler-wardhaugh/lua/test.lua new file mode 100755 index 0000000000..f2071e44f2 --- /dev/null +++ b/challenge-096/tyler-wardhaugh/lua/test.lua @@ -0,0 +1,21 @@ +#!/usr/bin/env lua + +require 'busted.runner'() + +describe("Task 1, Reverse Words", function() + local t1 = require'ch-1' + it("produces correct results for the examples", function() + assert.are.same("Challenge Weekly The", t1.reverse_words("The Weekly Challenge")) + assert.are.same("family same the of part are Raku and Perl", + t1.reverse_words(" Perl and Raku are part of the same family ")) + end) +end) + + +describe("Task 2, Edit Distance", function() + local t2 = require'ch-2' + it("produces correct results for the examples", function() + assert.are.same(3, t2.edit_distance("kitten", "sitting")) + assert.are.same(2, t2.edit_distance("sunday", "monday")) + end) +end) |
