aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-07-15 14:03:49 +0100
committerGitHub <noreply@github.com>2021-07-15 14:03:49 +0100
commitd40ef9619bcfd1a5e03c8e59ceb3407b929fc851 (patch)
tree3292dcbc715ed0acd5208038cfc5b9ad89d6de0c
parente283984f3587815b9a2a49510e5e649a4b88d646 (diff)
parentd7c92b61bd7e0a035ea8ec7f4e515d047982d4a8 (diff)
downloadperlweeklychallenge-club-d40ef9619bcfd1a5e03c8e59ceb3407b929fc851.tar.gz
perlweeklychallenge-club-d40ef9619bcfd1a5e03c8e59ceb3407b929fc851.tar.bz2
perlweeklychallenge-club-d40ef9619bcfd1a5e03c8e59ceb3407b929fc851.zip
Merge pull request #4525 from stuart-little/stuart-little_101_lua
1st commit on 101_lua
-rwxr-xr-xchallenge-101/stuart-little/lua/ch-1.lua40
-rwxr-xr-xchallenge-101/stuart-little/lua/ch-2.lua16
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-101/stuart-little/lua/ch-1.lua b/challenge-101/stuart-little/lua/ch-1.lua
new file mode 100755
index 0000000000..e73bbb0265
--- /dev/null
+++ b/challenge-101/stuart-little/lua/ch-1.lua
@@ -0,0 +1,40 @@
+#!/usr/bin/env lua
+
+-- run <script> <space-separated array entries>
+
+function dims(n)
+ local rows = 1
+ for i=1,math.sqrt(n) do
+ if n % i == 0 then rows=i end
+ end
+ return rows, math.floor(n/rows)
+end
+
+function rot(t)
+ if #t==0 then return {} end
+ local rt={}
+ for i=#(t[1]),1,-1 do
+ local row={}
+ for j=1,#t do table.insert(row,t[j][i]) end
+ table.insert(rt,row)
+ end
+ return rt
+end
+
+function spiral(t,rows,cols)
+ local sp={}
+ if rows*cols==0 then return sp end
+ table.insert(sp,table.pack(table.unpack(t,1,cols)))
+ local rest = rot(spiral(table.pack(table.unpack(t,cols+1)),cols,rows-1))
+ for k,v in ipairs(rest) do
+ table.insert(sp,k,v)
+ end
+ return sp
+end
+
+function ppMat(t)
+ for _,v in ipairs(t) do print(table.unpack(v)) end
+end
+
+local rows,cols=dims(#arg)
+ppMat(spiral(arg,rows,cols))
diff --git a/challenge-101/stuart-little/lua/ch-2.lua b/challenge-101/stuart-little/lua/ch-2.lua
new file mode 100755
index 0000000000..2f2b02b6ef
--- /dev/null
+++ b/challenge-101/stuart-little/lua/ch-2.lua
@@ -0,0 +1,16 @@
+#!/usr/bin/env lua
+
+-- run <script> <x1 y1 x2 y2 x3 y3>
+
+function contains0(t)
+ local lt90=0
+ local tt = table.pack(table.unpack(t))
+ for i=1,2 do table.insert(tt,tt[i]) end
+ for i=1,3 do
+ local dotP=tt[2*i-1]*tt[2*i+1] + tt[2*i]*tt[2*i+2]
+ if dotP > 0 then lt90=lt90+1 end
+ end
+ return lt90 >= 2 and 0 or 1
+end
+
+print(contains0(table.pack(table.unpack(arg,1,6))))