diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-11-21 04:57:01 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-21 04:57:01 +0000 |
| commit | 054a1d856f6600be605d149f7830a7426f828fce (patch) | |
| tree | ee38a9bf6eef686793ab957d964d2c70cb4d3ee7 /challenge-191/deadmarshal/lua | |
| parent | 7ac92a4a7fd76a307feefe75fcb666f9525e1b7b (diff) | |
| parent | 4a6109910c67852630b4d5b50d2a1bf465cfbeaa (diff) | |
| download | perlweeklychallenge-club-054a1d856f6600be605d149f7830a7426f828fce.tar.gz perlweeklychallenge-club-054a1d856f6600be605d149f7830a7426f828fce.tar.bz2 perlweeklychallenge-club-054a1d856f6600be605d149f7830a7426f828fce.zip | |
Merge pull request #7110 from deadmarshal/challenge191
Challenge191
Diffstat (limited to 'challenge-191/deadmarshal/lua')
| -rw-r--r-- | challenge-191/deadmarshal/lua/ch-1.lua | 11 | ||||
| -rw-r--r-- | challenge-191/deadmarshal/lua/ch-2.lua | 28 |
2 files changed, 39 insertions, 0 deletions
diff --git a/challenge-191/deadmarshal/lua/ch-1.lua b/challenge-191/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..ced87fe681 --- /dev/null +++ b/challenge-191/deadmarshal/lua/ch-1.lua @@ -0,0 +1,11 @@ +local function twice_largest(t) + table.sort(t, function(a,b) return a > b end) + if t[1] >= 2 * t[2] then return 1 end + return -1 +end + +print(string.format("%2d", twice_largest({1,2,3,4}))) +print(string.format("%2d", twice_largest({1,2,0,5}))) +print(string.format("%2d", twice_largest({2,6,3,1}))) +print(string.format("%2d", twice_largest({4,5,2,3}))) + diff --git a/challenge-191/deadmarshal/lua/ch-2.lua b/challenge-191/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..0c6131362a --- /dev/null +++ b/challenge-191/deadmarshal/lua/ch-2.lua @@ -0,0 +1,28 @@ +local function is_cute(t) + assert(type(t) == 'table', 't must be a table!') + for i=1, #t do + if i % t[i] ~= 0 and t[i] % i ~= 0 then return false end + end + return true +end + +local function permute(t, i, sz, t2) + assert(type(t) == 'table' and + type(i) == 'number' and + type(sz) == 'number' and + type(t2) == 'table', 'params assertions failed!') + if sz == i then + if is_cute(t) then t2.count = t2.count + 1 return end + end + for j=i, sz do + t[i], t[j] = t[j], t[i] + permute(t,i+1,sz,t2) + t[i], t[j] = t[j], t[i] + end + return +end + +local t,t2 = {1,2},{count = 0} +permute(t, 1, 2, t2) +print(string.format("%d",t2.count)) + |
