diff options
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} + |
