aboutsummaryrefslogtreecommitdiff
path: root/challenge-238/deadmarshal/lua
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-10-12 21:13:29 +0100
committerGitHub <noreply@github.com>2023-10-12 21:13:29 +0100
commit564829406b4704b821beb8940fe05fea5f302b01 (patch)
tree837a50917d24027d2ca837ba7128c35b96c47266 /challenge-238/deadmarshal/lua
parent0c4b179a018dfe100576dd2c147fb734227288e8 (diff)
parenta6233090f6700d2c4b34d30c9e50c6b113b722d2 (diff)
downloadperlweeklychallenge-club-564829406b4704b821beb8940fe05fea5f302b01.tar.gz
perlweeklychallenge-club-564829406b4704b821beb8940fe05fea5f302b01.tar.bz2
perlweeklychallenge-club-564829406b4704b821beb8940fe05fea5f302b01.zip
Merge pull request #8860 from deadmarshal/TWC238
TWC238
Diffstat (limited to 'challenge-238/deadmarshal/lua')
-rw-r--r--challenge-238/deadmarshal/lua/ch-1.lua17
-rw-r--r--challenge-238/deadmarshal/lua/ch-2.lua32
2 files changed, 49 insertions, 0 deletions
diff --git a/challenge-238/deadmarshal/lua/ch-1.lua b/challenge-238/deadmarshal/lua/ch-1.lua
new file mode 100644
index 0000000000..d0e626fcdb
--- /dev/null
+++ b/challenge-238/deadmarshal/lua/ch-1.lua
@@ -0,0 +1,17 @@
+#!/usr/bin/env lua
+
+local function running_sum(t)
+ assert(type(t) == 'table','t must be a table!')
+ local ret = {}
+ local sum = 0
+ for i=1,#t do
+ sum = sum + t[i]
+ ret[#ret+1] = sum
+ end
+ return ret
+end
+
+print(table.unpack(running_sum{1,2,3,4,5}))
+print(table.unpack(running_sum{1,1,1,1,1}))
+print(table.unpack(running_sum{0,-1,1,2}))
+
diff --git a/challenge-238/deadmarshal/lua/ch-2.lua b/challenge-238/deadmarshal/lua/ch-2.lua
new file mode 100644
index 0000000000..660235f0bb
--- /dev/null
+++ b/challenge-238/deadmarshal/lua/ch-2.lua
@@ -0,0 +1,32 @@
+#!/usr/bin/env lua
+
+local function product(n)
+ assert(type(n) == 'number','n must be a number!')
+ local prod = 1
+ while n > 0 do
+ prod = prod * (n % 10)
+ n = n // 10
+ end
+ return prod
+end
+
+local function helper(n)
+ assert(type(n) == 'number','n must be a number!')
+ local sum = 0
+ repeat
+ sum = sum + 1
+ n = product(n)
+ until n < 10
+ return sum
+end
+
+local function persistence_sort(t)
+ assert(type(t) == 'table','t must be a table!')
+ table.sort(t,function(a,b) return helper(a) < helper(b) or
+ a < b end)
+ print(table.unpack(t))
+end
+
+persistence_sort{15,99,1,34}
+persistence_sort{50,25,33,22}
+