aboutsummaryrefslogtreecommitdiff
path: root/challenge-211/roger-bell-west/lua
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-07 01:32:52 +0100
committerGitHub <noreply@github.com>2023-04-07 01:32:52 +0100
commit4832e5912bf07b2bad3aff3156c4ccc1bc01d830 (patch)
treedae885a4f949f70f58c22542ccb08ed7a3f8b31a /challenge-211/roger-bell-west/lua
parentc76c373fdf70d1b34420d0108f616eac18fc1c8e (diff)
parent22437e2fa18fda4713254cc11efdc21f4ece082a (diff)
downloadperlweeklychallenge-club-4832e5912bf07b2bad3aff3156c4ccc1bc01d830.tar.gz
perlweeklychallenge-club-4832e5912bf07b2bad3aff3156c4ccc1bc01d830.tar.bz2
perlweeklychallenge-club-4832e5912bf07b2bad3aff3156c4ccc1bc01d830.zip
Merge pull request #7852 from Firedrake/rogerbw-challenge-211
RogerBW solutions for challenge no. 211
Diffstat (limited to 'challenge-211/roger-bell-west/lua')
-rwxr-xr-xchallenge-211/roger-bell-west/lua/ch-1.lua46
-rwxr-xr-xchallenge-211/roger-bell-west/lua/ch-2.lua78
2 files changed, 124 insertions, 0 deletions
diff --git a/challenge-211/roger-bell-west/lua/ch-1.lua b/challenge-211/roger-bell-west/lua/ch-1.lua
new file mode 100755
index 0000000000..50dfb58b1c
--- /dev/null
+++ b/challenge-211/roger-bell-west/lua/ch-1.lua
@@ -0,0 +1,46 @@
+#! /usr/bin/lua
+
+function toeplitzmatrix(a)
+ local ym = #a - 1
+ local xm = #(a[1]) - 1
+ local toeplitz = true
+ for xb = (1 - xm), (ym - 1) do
+ local init = true
+ local tv = 0
+ for xi = xb, xb + xm do
+ if xi >= 0 and xi <= xm then
+ local x = xi + 1
+ local yi = xi - xb
+ if yi >= 0 and yi <= ym then
+ local y = yi + 1
+ if init then
+ init = false
+ tv = a[y][x]
+ elseif a[y][x] ~= tv then
+ toeplitz = false
+ break
+ end
+ end
+ end
+ end
+ if not toeplitz then
+ break
+ end
+ end
+ return toeplitz
+end
+
+if toeplitzmatrix({{4, 3, 2, 1}, {5, 4, 3, 2}, {6, 5, 4, 3}}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if not toeplitzmatrix({{1, 2, 3}, {3, 2, 1}}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
+
diff --git a/challenge-211/roger-bell-west/lua/ch-2.lua b/challenge-211/roger-bell-west/lua/ch-2.lua
new file mode 100755
index 0000000000..c524e76c6d
--- /dev/null
+++ b/challenge-211/roger-bell-west/lua/ch-2.lua
@@ -0,0 +1,78 @@
+#! /usr/bin/lua
+
+function combinations(arr, k)
+ local c = {}
+ for i = 1, k do
+ table.insert(c, i)
+ end
+ table.insert(c, #arr + 1)
+ table.insert(c, 0)
+ local out = {}
+ while true do
+ local inner = {}
+ for i = k, 1, -1 do
+ table.insert(inner, arr[c[i]])
+ end
+ table.insert(out, inner)
+ local j = 1
+ while c[j] + 1 == c[j + 1] do
+ c[j] = j
+ j = j + 1
+ end
+ if j > k then
+ break
+ end
+ c[j] = c[j] + 1
+ end
+ return ipairs(out)
+end
+
+function sum(t)
+ local ss = 0
+ for i, k in ipairs(t) do
+ ss = ss + k
+ end
+ return ss
+end
+
+function splitsameaverage(a)
+ local ss = sum(a)
+ local ml = #a
+ local mx = ml // 2
+ local ssa = false
+ for n = 1, mx do
+ for i, c in combinations(a, n) do
+ local ca = sum(c)
+ if (ca / n) == (ss - ca) / (ml - n) then
+ ssa = true
+ break
+ end
+ end
+ if ssa then
+ break
+ end
+ end
+ return ssa
+end
+
+if splitsameaverage({1, 2, 3, 4, 5, 6, 7, 8}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if not splitsameaverage({1, 3}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if splitsameaverage({1, 2, 3}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
+