diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-13 15:45:00 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-13 15:45:00 +0100 |
| commit | 52723f74a6fc8680e19ea79ddd01bae1d9e47829 (patch) | |
| tree | 9bde73f6bd6e38f0e6bbfb0f6854e09076ef52aa /challenge-160/deadmarshal/lua/ch-2.lua | |
| parent | 1f611de5f4a63b384e9721f184fc770acf7797a8 (diff) | |
| parent | aad6c403c706dc507f36e573894874ffb0301503 (diff) | |
| download | perlweeklychallenge-club-52723f74a6fc8680e19ea79ddd01bae1d9e47829.tar.gz perlweeklychallenge-club-52723f74a6fc8680e19ea79ddd01bae1d9e47829.tar.bz2 perlweeklychallenge-club-52723f74a6fc8680e19ea79ddd01bae1d9e47829.zip | |
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-160/deadmarshal/lua/ch-2.lua')
| -rw-r--r-- | challenge-160/deadmarshal/lua/ch-2.lua | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/challenge-160/deadmarshal/lua/ch-2.lua b/challenge-160/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..9682d9c617 --- /dev/null +++ b/challenge-160/deadmarshal/lua/ch-2.lua @@ -0,0 +1,24 @@ +function array_sum(t) + assert(type(t) == "table", "t must be a table!") + local sum = 0 + for i=1, #t do sum = sum + t[i] end + return sum +end + +function equilibrium_index(t) + assert(type(t) == "table", "t must be a table!") + local left, right, ret = 0, array_sum(t), -1 + for i,j in pairs(t) do + right = right - j + if left == right then + ret = i + break + end + left = left + j + end + return ret +end + +print(equilibrium_index({1,3,5,7,9})) +print(equilibrium_index({1,2,3,4,5})) +print(equilibrium_index({2,4,2})) |
