diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-11-25 15:30:20 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-25 15:30:20 +0000 |
| commit | becc2f759b6b157ae4bb5ca61f11a99727889846 (patch) | |
| tree | 06a50ee1a6729649b28c7655b9d1bf1f9f5f780e /challenge-192/deadmarshal/lua | |
| parent | df71a9b3469ce5357435f500d007664a12d0db4e (diff) | |
| parent | 00f33741e0e36c9ef65bc315138b48bb9a31ac42 (diff) | |
| download | perlweeklychallenge-club-becc2f759b6b157ae4bb5ca61f11a99727889846.tar.gz perlweeklychallenge-club-becc2f759b6b157ae4bb5ca61f11a99727889846.tar.bz2 perlweeklychallenge-club-becc2f759b6b157ae4bb5ca61f11a99727889846.zip | |
Merge pull request #7150 from deadmarshal/challenge192
Challenge192
Diffstat (limited to 'challenge-192/deadmarshal/lua')
| -rw-r--r-- | challenge-192/deadmarshal/lua/ch-1.lua | 9 | ||||
| -rw-r--r-- | challenge-192/deadmarshal/lua/ch-2.lua | 25 |
2 files changed, 34 insertions, 0 deletions
diff --git a/challenge-192/deadmarshal/lua/ch-1.lua b/challenge-192/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..24ce490784 --- /dev/null +++ b/challenge-192/deadmarshal/lua/ch-1.lua @@ -0,0 +1,9 @@ +local function binary_flip(n) + assert(type(n) == 'number', 'n must be a number!') + return (~n) & (2 ^ (math.log(n) // math.log(2))-1) +end + +print(binary_flip(5)) +print(binary_flip(4)) +print(binary_flip(6)) + diff --git a/challenge-192/deadmarshal/lua/ch-2.lua b/challenge-192/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..9fb45e9023 --- /dev/null +++ b/challenge-192/deadmarshal/lua/ch-2.lua @@ -0,0 +1,25 @@ +local function table_sum(t) + local sum = 0 + for i=1, #t do sum = sum + t[i] end + return sum +end + +local function equal_distribution(t) + assert(type(t) == 'table', 't must be a table!') + local sum = table_sum(t) + local avg,moves,sum_part = sum // #t,0,0 + if sum % #t == 0 then + for i=1, #t do + sum_part = sum_part + t[i] + moves = moves + math.abs(sum_part - (avg * (i))) + end + return moves + else + return -1 + end +end + +print(equal_distribution({1,0,5})) +print(equal_distribution({0,2,0})) +print(equal_distribution({0,3,0})) + |
