diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-10-12 21:13:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-12 21:13:29 +0100 |
| commit | 564829406b4704b821beb8940fe05fea5f302b01 (patch) | |
| tree | 837a50917d24027d2ca837ba7128c35b96c47266 /challenge-238/deadmarshal/lua/ch-2.lua | |
| parent | 0c4b179a018dfe100576dd2c147fb734227288e8 (diff) | |
| parent | a6233090f6700d2c4b34d30c9e50c6b113b722d2 (diff) | |
| download | perlweeklychallenge-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/ch-2.lua')
| -rw-r--r-- | challenge-238/deadmarshal/lua/ch-2.lua | 32 |
1 files changed, 32 insertions, 0 deletions
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} + |
