diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-10-01 07:08:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-01 07:08:29 +0100 |
| commit | 236626df73f6a77ffa3a9ef434b6a05b88896698 (patch) | |
| tree | 5d017ee8c9c01b80c7de982b4cf9983fb40e0067 /challenge-080/tyler-wardhaugh/lua/ch-2.lua | |
| parent | 43c5d6e2a1d02048e68f6aaddd874137b140f154 (diff) | |
| parent | 9f221c40fce21616bca718b77baede972affd2a9 (diff) | |
| download | perlweeklychallenge-club-236626df73f6a77ffa3a9ef434b6a05b88896698.tar.gz perlweeklychallenge-club-236626df73f6a77ffa3a9ef434b6a05b88896698.tar.bz2 perlweeklychallenge-club-236626df73f6a77ffa3a9ef434b6a05b88896698.zip | |
Merge pull request #2422 from tylerw/tw/challenge-080-lua
Ch80: add Lua solutions
Diffstat (limited to 'challenge-080/tyler-wardhaugh/lua/ch-2.lua')
| -rwxr-xr-x | challenge-080/tyler-wardhaugh/lua/ch-2.lua | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/challenge-080/tyler-wardhaugh/lua/ch-2.lua b/challenge-080/tyler-wardhaugh/lua/ch-2.lua new file mode 100755 index 0000000000..54d355d037 --- /dev/null +++ b/challenge-080/tyler-wardhaugh/lua/ch-2.lua @@ -0,0 +1,25 @@ +#!/usr/bin/env lua + +local t2 = {} + +function t2.count_candies(coll) + local count = #coll + local maybe_inc = function(i, j) + if (coll[i] > coll[j]) then count = count + 1 end + end + + -- sweep left-to-right + for i=1,#coll-1 do maybe_inc(i, i+1) end + -- sweep right-to-left + for i=#coll,2,-1 do maybe_inc(i, i-1) end + + return count +end + +function t2.run(arg) + local N = {} + for _, v in ipairs(arg) do table.insert(N, tonumber(v)) end + print(t2.count_candies(N)) +end + +return t2 |
