aboutsummaryrefslogtreecommitdiff
path: root/challenge-228/deadmarshal/lua
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-07-31 23:24:51 +0100
committerGitHub <noreply@github.com>2023-07-31 23:24:51 +0100
commitc6042e89c554d72819b279c124b0177bcab5edfe (patch)
treea0cc611cd3501e32897d76b07c969612cb846834 /challenge-228/deadmarshal/lua
parent43017c7a64e42e35d855c4dc22bfaf819816e70a (diff)
parent0defc5eb9c087a7f8362b9d67f0020235734d2a9 (diff)
downloadperlweeklychallenge-club-c6042e89c554d72819b279c124b0177bcab5edfe.tar.gz
perlweeklychallenge-club-c6042e89c554d72819b279c124b0177bcab5edfe.tar.bz2
perlweeklychallenge-club-c6042e89c554d72819b279c124b0177bcab5edfe.zip
Merge pull request #8477 from deadmarshal/TWC228
TWC228
Diffstat (limited to 'challenge-228/deadmarshal/lua')
-rw-r--r--challenge-228/deadmarshal/lua/ch-1.lua18
-rw-r--r--challenge-228/deadmarshal/lua/ch-2.lua15
2 files changed, 33 insertions, 0 deletions
diff --git a/challenge-228/deadmarshal/lua/ch-1.lua b/challenge-228/deadmarshal/lua/ch-1.lua
new file mode 100644
index 0000000000..0de103fe4e
--- /dev/null
+++ b/challenge-228/deadmarshal/lua/ch-1.lua
@@ -0,0 +1,18 @@
+#!/usr/bin/env lua
+
+local function unique_sum(t)
+ local hash,sum = {},0
+ setmetatable(hash,{__index = function(t,k) return 0 end})
+ for i=1,#t do
+ hash[t[i]] = hash[t[i]] + 1
+ end
+ for k,v in pairs(hash) do
+ if v == 1 then sum = sum + k end
+ end
+ return sum
+end
+
+print(unique_sum({2,1,3,2}))
+print(unique_sum({1,1,1,1}))
+print(unique_sum({2,1,3,4}))
+
diff --git a/challenge-228/deadmarshal/lua/ch-2.lua b/challenge-228/deadmarshal/lua/ch-2.lua
new file mode 100644
index 0000000000..8f3245f208
--- /dev/null
+++ b/challenge-228/deadmarshal/lua/ch-2.lua
@@ -0,0 +1,15 @@
+#!/usr/bin/env lua
+
+local function empty_array(t)
+ local count = 0
+ while #t ~= 0 do
+ if t[1] == math.min(table.unpack(t)) then table.remove(t,1)
+ else table.insert(t,table.remove(t,1)) end
+ count = count + 1
+ end
+ return count
+end
+
+print(empty_array({3,4,2}))
+print(empty_array({1,2,3}))
+