diff options
| -rw-r--r-- | challenge-345/pokgopun/lua/ch-1.lua | 87 | ||||
| -rw-r--r-- | challenge-345/pokgopun/lua/ch-2.lua | 105 |
2 files changed, 192 insertions, 0 deletions
diff --git a/challenge-345/pokgopun/lua/ch-1.lua b/challenge-345/pokgopun/lua/ch-1.lua new file mode 100644 index 0000000000..29bcf48912 --- /dev/null +++ b/challenge-345/pokgopun/lua/ch-1.lua @@ -0,0 +1,87 @@ +--# https://theweeklychallenge.org/blog/perl-weekly-challenge-345/ +--[[ + +Task 1: Peak Positions + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Find all the peaks in the array, a peak is an element that is strictly + greater than its left and right neighbours. Return the indices of all + such peak positions. + +Example 1 + +Input: @ints = (1, 3, 2) +Output: (1) + +Example 2 + +Input: @ints = (2, 4, 6, 5, 3) +Output: (2) + +Example 3 + +Input: @ints = (1, 2, 3, 2, 4, 1) +Output: (2, 4) + +Example 4 + +Input: @ints = (5, 3, 1) +Output: (0) + +Example 5 + +Input: @ints = (1, 5, 1, 5, 1, 5, 1) +Output: (1, 3, 5) + +Task 2: Last Visitor +--]] +--# solution by pokgopun@gmail.com + +--@params table{int} +function pp(ints) --@return table{int} + l = #ints + if l == 0 then + return {} + elseif l == 1 then + return {0} + end + i = 1 + p = {} + while i <= l do + if i == l then + table.insert(p,i-1) + break + end + if ints[i] < ints[i+1] then + i = i + 1 + else + if ints[i] > ints[i+1] then + table.insert(p,i-1) + end + i = i + 2 + if i == l and ints[i] <= ints[i-1] then + break + end + end + end + return p +end + +lu = require("luaunit") + +function TestPp() + for _, data in pairs({ + {{1, 3, 2}, {1}}, + {{2, 4, 6, 5, 3}, {2}}, + {{1, 2, 3, 2, 4, 1}, {2, 4}}, + {{5, 3, 1}, {0}}, + {{1, 5, 1, 5, 1, 5, 1}, {1, 3, 5}}}) do + lu.assertEquals(pp(data[1]),data[2]) + end +end + +lu.run() diff --git a/challenge-345/pokgopun/lua/ch-2.lua b/challenge-345/pokgopun/lua/ch-2.lua new file mode 100644 index 0000000000..7abbd8861d --- /dev/null +++ b/challenge-345/pokgopun/lua/ch-2.lua @@ -0,0 +1,105 @@ +--# https://theweeklychallenge.org/blog/perl-weekly-challenge-345/ +--[[ + +Task 2: Last Visitor + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an integer array @ints where each element is either a + positive integer or -1. + + We process the array from left to right while maintaining two lists: +@seen: stores previously seen positive integers (newest at the front) +@ans: stores the answers for each -1 + + Rules: +If $ints[i] is a positive number -> insert it at the front of @seen +If $ints[i] is -1: + + Let $x be how many -1s in a row we’ve seen before this one. + + If $x < len(@seen) -> append seen[x] to @ans + + Else -> append -1 to @ans + + At the end, return @ans. + +Example 1 + +Input: @ints = (5, -1, -1) +Output: (5, -1) + +@seen = (5) +First -1: @ans = (5) +Second -1: @ans = (5, -1) + +Example 2 + +Input: @ints = (3, 7, -1, -1, -1) +Output: (7, 3, -1) + +@seen = (3, 7) +First -1: @ans = (7) +Second -1: @ans = (7, 3) +Third -1: @ans = (7, 3, -1) + +Example 3 + +Input: @ints = (2, -1, 4, -1, -1) +Output: (2, 4, 2) + +Example 4 + +Input: @ints = (10, 20, -1, 30, -1, -1) +Output: (20, 30, 20) + +Example 5 + +Input: @ints = (-1, -1, 5, -1) +Output: (-1, -1, 5) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 2nd November + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +--]] +--# solution by pokgopun@gmail.com + +--@params table{int} +function lv(ints) --@return table{int} + seen = {} --table{int} + ans = {} --table{int} + x = 0 + for _, v in ipairs(ints) do + if v == -1 then + if x < #seen then + table.insert(ans,seen[x+1]) + else + table.insert(ans,-1) + end + x = x + 1 + else + x = 0 + table.insert(seen,1,v) + end + end + return ans +end + +lu = require("luaunit") + +function TestLv() + for _, data in pairs({ + { Input= {5, -1, -1}, Output= {5, -1}}, + {Input= {3, 7, -1, -1, -1}, Output= {7, 3, -1}}, + {Input= {2, -1, 4, -1, -1},Output= {2, 4, 2}}, + {Input= {10, 20, -1, 30, -1, -1}, Output= {20, 30, 20}}, + {Input= {-1, -1, 5, -1},Output= {-1, -1, 5}}}) do + lu.assertEquals(lv(data.Input), data.Output) + end +end + +lu.run() |
