diff options
| author | 冯昶 <fengchang@novel-supertv.com> | 2023-07-24 17:16:48 +0800 |
|---|---|---|
| committer | 冯昶 <fengchang@novel-supertv.com> | 2023-07-24 17:16:48 +0800 |
| commit | 4fda4a4a398e64921020704733556a2ec6dae78a (patch) | |
| tree | f2b70bccff1e2d183104042921d0726f7a8e5f40 /challenge-226/deadmarshal/lua/ch-2.lua | |
| parent | a296c2a161ce1fffe7c7c1108346c81f027af419 (diff) | |
| parent | e4bdf5dcb6e741f1fb8e1b145fd2111f05ed6445 (diff) | |
| download | perlweeklychallenge-club-4fda4a4a398e64921020704733556a2ec6dae78a.tar.gz perlweeklychallenge-club-4fda4a4a398e64921020704733556a2ec6dae78a.tar.bz2 perlweeklychallenge-club-4fda4a4a398e64921020704733556a2ec6dae78a.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-226/deadmarshal/lua/ch-2.lua')
| -rw-r--r-- | challenge-226/deadmarshal/lua/ch-2.lua | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/challenge-226/deadmarshal/lua/ch-2.lua b/challenge-226/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..11a97a94bd --- /dev/null +++ b/challenge-226/deadmarshal/lua/ch-2.lua @@ -0,0 +1,24 @@ +#!/usr/bin/env lua + +local function deduplicate_and_remove_zeros(t) + assert(type(t) == 'table','t must be a table') + local res,hash = {},{} + for i=1,#t do + if t[i] == 0 then goto continue end + if not hash[t[i]] then res[#res+1] = t[i] end + hash[t[i]] = true + ::continue:: + end + return res +end + +local function zero_array(t) + assert(type(t) == 'table','t must be a table') + local dedup = deduplicate_and_remove_zeros(t) + return #dedup +end + +print(zero_array({1,5,0,3,5})) +print(zero_array({0})) +print(zero_array({2,1,4,0,3})) + |
