aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-09-16 13:26:03 -0700
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-09-17 14:56:29 -0700
commitefe327762628e2daddfd8848746371fce3d78a7d (patch)
treee31ae769edea36fa1519c9a3613a70a6d0a6c31c
parent3002d90e698025326fe8cc609da084b9ea5bead1 (diff)
downloadperlweeklychallenge-club-efe327762628e2daddfd8848746371fce3d78a7d.tar.gz
perlweeklychallenge-club-efe327762628e2daddfd8848746371fce3d78a7d.tar.bz2
perlweeklychallenge-club-efe327762628e2daddfd8848746371fce3d78a7d.zip
implement Task 2 in Lua
-rw-r--r--challenge-078/tyler-wardhaugh/lua/README.md4
-rwxr-xr-xchallenge-078/tyler-wardhaugh/lua/ch-2.lua22
-rwxr-xr-xchallenge-078/tyler-wardhaugh/lua/test.lua14
3 files changed, 40 insertions, 0 deletions
diff --git a/challenge-078/tyler-wardhaugh/lua/README.md b/challenge-078/tyler-wardhaugh/lua/README.md
index 741ee6bff1..91e2f8575d 100644
--- a/challenge-078/tyler-wardhaugh/lua/README.md
+++ b/challenge-078/tyler-wardhaugh/lua/README.md
@@ -9,6 +9,10 @@ Run Task 1:
$ lua -e 'require"ch-1".run({9, 10, 7, 5, 6, 1})'
+Run Task 2:
+
+ $ lua -e 'require"ch-2".run({10, 20, 30, 40, 50}, {3, 4})'
+
Run the project's tests (which are samples from the task descriptions):
$ lua test.lua
diff --git a/challenge-078/tyler-wardhaugh/lua/ch-2.lua b/challenge-078/tyler-wardhaugh/lua/ch-2.lua
new file mode 100755
index 0000000000..f5b2ab901a
--- /dev/null
+++ b/challenge-078/tyler-wardhaugh/lua/ch-2.lua
@@ -0,0 +1,22 @@
+#!/usr/bin/env lua
+
+local t2 = {}
+
+function t2.multi_rotate(list, rotations)
+ local rts = {}
+ for _, rv in ipairs(rotations) do
+ local rotated = table.move(list, rv + 1, #list, 1, {})
+ for i = 1, rv do table.insert(rotated, list[i]) end
+ table.insert(rts, rotated)
+ end
+ return rts
+end
+
+function t2.run(...)
+ local res = t2.multi_rotate(...)
+ for _, v in ipairs(res) do
+ print('[' .. table.concat(v, ' ') .. ']')
+ end
+end
+
+return t2
diff --git a/challenge-078/tyler-wardhaugh/lua/test.lua b/challenge-078/tyler-wardhaugh/lua/test.lua
index d47309f705..c5494095c0 100755
--- a/challenge-078/tyler-wardhaugh/lua/test.lua
+++ b/challenge-078/tyler-wardhaugh/lua/test.lua
@@ -9,3 +9,17 @@ describe("Task 1, Leader Elements", function()
assert.are.same({5}, t1.leaders({3, 4, 5}))
end)
end)
+
+describe("Task 2, Left Rotation", function()
+ local t2 = require'ch-2'
+ it("produces correct results for the examples", function()
+ assert.are.same(
+ {{40, 50, 10, 20, 30}, {50, 10, 20, 30, 40}},
+ t2.multi_rotate({10, 20, 30, 40, 50}, {3, 4})
+ )
+ assert.are.same(
+ {{4, 2, 6, 3, 7}, {6, 3, 7, 4, 2}, {3, 7, 4, 2, 6}},
+ t2.multi_rotate({7, 4, 2, 6, 3}, {1, 3, 4})
+ )
+ end)
+end)