aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-11-29 17:39:46 +0000
committerGitHub <noreply@github.com>2020-11-29 17:39:46 +0000
commit8f4c1b97453d02492a7738b1f0cb6c7358af4ae9 (patch)
tree356c160f2fc58ac860606bdaf58981b91c014b62
parentc0eb54b0a2136f6be4ae687bcd7670a40e218464 (diff)
parent9bb9704b4cf33649c0cfee6c6e3af6a7297de421 (diff)
downloadperlweeklychallenge-club-8f4c1b97453d02492a7738b1f0cb6c7358af4ae9.tar.gz
perlweeklychallenge-club-8f4c1b97453d02492a7738b1f0cb6c7358af4ae9.tar.bz2
perlweeklychallenge-club-8f4c1b97453d02492a7738b1f0cb6c7358af4ae9.zip
Merge pull request #2864 from tylerw/tw/challenge-088
Challenge 088 in Lua
-rw-r--r--challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t1.clj15
-rwxr-xr-xchallenge-088/tyler-wardhaugh/lua/ch-1.lua40
-rwxr-xr-xchallenge-088/tyler-wardhaugh/lua/ch-2.lua55
-rwxr-xr-xchallenge-088/tyler-wardhaugh/lua/test.lua24
-rw-r--r--challenge-088/tyler-wardhaugh/lua/util.lua31
5 files changed, 162 insertions, 3 deletions
diff --git a/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t1.clj b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t1.clj
index 354eef276d..968eed77ad 100644
--- a/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t1.clj
+++ b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t1.clj
@@ -14,11 +14,20 @@
product (transduce (map (partial apply math/expt)) * freqs)
cache-xf (map (juxt key (comp (partial / product) key)))
cache (into {} cache-xf freqs)]
- (sequence (map cache) coll)))
+ (map cache coll)))
+
+(defn format-output
+ "Print the output according to the task description"
+ ([input products] (format-output true input products))
+ ([stream input products]
+ (let [swap-out (fn [i] (concat (subvec input 0 i) (subvec input (inc i))))]
+ (cl-format stream "@M = (~{~a~^, ~})~%~%" products)
+ (cl-format stream "~:{$M[~a] = ~{~a~^ x ~} = ~a~%~}"
+ (map-indexed (fn [i v] [i (swap-out i) v]) products)))))
(defn -main
"Run Task 1 with a list of numbers N, defaulting to the
first example given in the task description."
[& args]
- (let [N (or (some->> args (map edn/read-string)) [5 2 1 4 3])]
- (cl-format true "@M = (~{~a~^, ~})" (array-of-product N))))
+ (let [N (or (some->> args (mapv edn/read-string)) [5 2 1 4 3])]
+ (format-output N (array-of-product N))))
diff --git a/challenge-088/tyler-wardhaugh/lua/ch-1.lua b/challenge-088/tyler-wardhaugh/lua/ch-1.lua
new file mode 100755
index 0000000000..f1f1ac675b
--- /dev/null
+++ b/challenge-088/tyler-wardhaugh/lua/ch-1.lua
@@ -0,0 +1,40 @@
+#!/usr/bin/env lua
+
+local t1 = {}
+
+function t1.array_of_product(coll)
+ local product = 1
+ for i=2,#coll do product = product * coll[i] end
+
+ local result = {product}
+ for i=2,#coll do
+ product = product // coll[i] * coll[i-1]
+ table.insert(result, product)
+ end
+ return result
+end
+
+function t1.run(arg)
+ local N = {}
+ for _, v in ipairs(arg) do table.insert(N, tonumber(v)) end
+
+ M = t1.array_of_product(N)
+
+ io.write('@M = ( ')
+ io.write(table.concat(M, ', '))
+ print(' )\n')
+
+ for i, v in ipairs(M) do
+ io.write('$M[' .. i-1 .. '] = ')
+ local temp = {}
+ for j, vv in ipairs(N) do
+ if j ~= i then
+ table.insert(temp, vv)
+ end
+ end
+ io.write(table.concat(temp, ' x '))
+ print(' = ' .. v)
+ end
+end
+
+return t1
diff --git a/challenge-088/tyler-wardhaugh/lua/ch-2.lua b/challenge-088/tyler-wardhaugh/lua/ch-2.lua
new file mode 100755
index 0000000000..4f708ae420
--- /dev/null
+++ b/challenge-088/tyler-wardhaugh/lua/ch-2.lua
@@ -0,0 +1,55 @@
+#!/usr/bin/env lua
+
+local t2 = {}
+local util = require'util'
+
+function t2.reverse(coll)
+ local result = {}
+ for i=#coll,1,-1 do table.insert(result, coll[i]) end
+ return result
+end
+
+function t2.transpose(matrix)
+ local result = {}
+ for i=1,#matrix[1] do
+ result[i] = {}
+ for j=1,#matrix do
+ result[i][j] = matrix[j][i]
+ end
+ end
+ return result
+end
+
+function t2.rotate(matrix)
+ local rotated = t2.reverse(t2.transpose(matrix))
+ return rotated
+end
+
+function t2.spiral(matrix)
+ local spiral_aux
+ spiral_aux = function(matrix, result)
+ if next(matrix) then
+ table.move(matrix[1], 1, #matrix[1], #result+1, result)
+ if #matrix > 2 then
+ table.remove(matrix, 1)
+ return spiral_aux(t2.rotate(matrix), result)
+ else
+ local reversed = t2.reverse(matrix[2])
+ table.move(reversed, 1, #reversed, #result+1, result)
+ return result
+ end
+ else
+ return result
+ end
+ end
+ return spiral_aux(matrix, {})
+end
+
+function t2.run(args)
+ local filename = args[1]
+ local matrix = util.parse_matrix_file(filename)
+ local spiraled = t2.spiral(matrix)
+ util.print_matrix({spiraled}, ', ')
+end
+
+return t2
diff --git a/challenge-088/tyler-wardhaugh/lua/test.lua b/challenge-088/tyler-wardhaugh/lua/test.lua
new file mode 100755
index 0000000000..7e3080b2ea
--- /dev/null
+++ b/challenge-088/tyler-wardhaugh/lua/test.lua
@@ -0,0 +1,24 @@
+#!/usr/bin/env lua
+
+require 'busted.runner'()
+MATRIX_FILE_PREFIX = '../clojure/resources/matrix-'
+
+describe("Task 1, Array of Product", function()
+ local t1 = require'ch-1'
+ it("produces correct results for the examples", function()
+ assert.are.same({24, 60, 120, 30, 40}, t1.array_of_product({5, 2, 1, 4, 3}))
+ assert.are.same({12, 24, 6, 8}, t1.array_of_product({2, 1, 4, 3}))
+ end)
+end)
+
+describe("Task 2, Spiral Matrix", function()
+ local t2 = require'ch-2'
+ local util = require'util'
+ local helper = function(n)
+ return t2.spiral(util.parse_matrix_file(MATRIX_FILE_PREFIX .. n))
+ end
+ it("produces correct results for the examples", function()
+ assert.are.same({1, 2, 3, 6, 9, 8, 7, 4, 5}, helper('1'))
+ assert.are.same({1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10}, helper('2'))
+ end)
+end)
diff --git a/challenge-088/tyler-wardhaugh/lua/util.lua b/challenge-088/tyler-wardhaugh/lua/util.lua
new file mode 100644
index 0000000000..2602cfe69b
--- /dev/null
+++ b/challenge-088/tyler-wardhaugh/lua/util.lua
@@ -0,0 +1,31 @@
+#!/usr/bin/env lua
+
+local util = {}
+
+function util.parse_matrix_file(filename)
+ local file = io.open(filename)
+ io.input(file)
+
+ local matrix = {}
+ for line in io.lines() do
+ local row = {}
+ for d in string.gmatch(line, '%d+') do
+ table.insert(row, math.tointeger(d))
+ end
+ table.insert(matrix, row)
+ end
+
+ return matrix
+end
+
+function util.print_matrix(matrix, sep)
+ sep = sep or ' '
+ for _, row in ipairs(matrix) do
+ io.write('[ ')
+ io.write(table.concat(row, sep))
+ io.write(' ]')
+ print()
+ end
+end
+
+return util