aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-27 23:10:05 +0000
committerGitHub <noreply@github.com>2025-10-27 23:10:05 +0000
commit3d02737b55d2a0192f967032f09928a05fdae39a (patch)
tree20927fd38861ed5bf2041e737e4c676d90987280
parentabe996e9ac2cb8caef4b993c2135a7f0d14bf455 (diff)
parent3caf220fdd4040113ddfeb8e43defb43d72bbb75 (diff)
downloadperlweeklychallenge-club-3d02737b55d2a0192f967032f09928a05fdae39a.tar.gz
perlweeklychallenge-club-3d02737b55d2a0192f967032f09928a05fdae39a.tar.bz2
perlweeklychallenge-club-3d02737b55d2a0192f967032f09928a05fdae39a.zip
Merge pull request #12932 from pokgopun/pwc345
Pwc345
-rw-r--r--challenge-345/pokgopun/go/ch-1.go96
-rw-r--r--challenge-345/pokgopun/go/ch-2.go113
-rw-r--r--challenge-345/pokgopun/lua/ch-1.lua87
-rw-r--r--challenge-345/pokgopun/lua/ch-2.lua105
-rw-r--r--challenge-345/pokgopun/python/ch-1.py85
-rw-r--r--challenge-345/pokgopun/python/ch-2.py101
6 files changed, 587 insertions, 0 deletions
diff --git a/challenge-345/pokgopun/go/ch-1.go b/challenge-345/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..ec1b9c1c03
--- /dev/null
+++ b/challenge-345/pokgopun/go/ch-1.go
@@ -0,0 +1,96 @@
+//# 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
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (in ints) process() ints {
+ l := len(in)
+ if l == 0 {
+ return ints{}
+ } else if l == 1 {
+ return ints{0}
+ }
+ i := 0
+ var p ints
+ for i < l {
+ if i == l-1 {
+ p = append(p, i)
+ break
+ }
+ if in[i] < in[i+1] {
+ i++
+ continue
+ }
+ if in[i] > in[i+1] {
+ p = append(p, i)
+ }
+ i += 2
+ if i == l-1 && in[i] <= in[i-1] {
+ break
+ }
+ }
+ return p
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output ints
+ }{
+ {ints{1, 3, 2}, ints{1}},
+ {ints{2, 4, 6, 5, 3}, ints{2}},
+ {ints{1, 2, 3, 2, 4, 1}, ints{2, 4}},
+ {ints{5, 3, 1}, ints{0}},
+ {ints{1, 5, 1, 5, 1, 5, 1}, ints{1, 3, 5}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-345/pokgopun/go/ch-2.go b/challenge-345/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..c850cf07d3
--- /dev/null
+++ b/challenge-345/pokgopun/go/ch-2.go
@@ -0,0 +1,113 @@
+//# 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
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (in ints) process() ints {
+ var seen, ans ints
+ x := 0
+ for _, v := range in {
+ if v == -1 {
+ if x < len(seen) {
+ ans = append(ans, seen[len(seen)-1-x])
+ } else {
+ ans = append(ans, -1)
+ }
+ x++
+ } else {
+ x = 0
+ seen = append(seen, v)
+ }
+ }
+ return ans
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output ints
+ }{
+ {ints{5, -1, -1}, ints{5, -1}},
+ {ints{3, 7, -1, -1, -1}, ints{7, 3, -1}},
+ {ints{2, -1, 4, -1, -1}, ints{2, 4, 2}},
+ {ints{10, 20, -1, 30, -1, -1}, ints{20, 30, 20}},
+ {ints{-1, -1, 5, -1}, ints{-1, -1, 5}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
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()
diff --git a/challenge-345/pokgopun/python/ch-1.py b/challenge-345/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..af603b166f
--- /dev/null
+++ b/challenge-345/pokgopun/python/ch-1.py
@@ -0,0 +1,85 @@
+### 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
+
+def pp(ints: tuple[int]) -> tuple[int]:
+ l = len(ints)
+ if l < 2:
+ return tuple(range(l))
+ i = 0
+ p = []
+ while i < l:
+ if i == l - 1:
+ p.append(i)
+ break
+ if ints[i] < ints[i+1]:
+ i += 1
+ continue
+ if ints[i] > ints[i+1]:
+ p.append(i)
+ i += 2
+ if i == l - 1 and ints[i] <= ints[i-1]:
+ break
+ return tuple(p)
+
+import unittest
+
+class TestPp(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ (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),
+ (2, 3, 4): (2,),
+ (2, 2, 2): (),
+ (3, 3): (),
+ (5, 3): (0,),
+ (5, 7): (1,),
+ (7,): (0,),
+ (): (),
+ }.items():
+ #print(inpt,otpt)
+ self.assertEqual(pp(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-345/pokgopun/python/ch-2.py b/challenge-345/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..dea0ecfc09
--- /dev/null
+++ b/challenge-345/pokgopun/python/ch-2.py
@@ -0,0 +1,101 @@
+### 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
+
+def lv(ints: tuple[int]) -> tuple[int]:
+ seen = list()
+ ans = list()
+ x = 0
+ for v in ints:
+ if v == -1:
+ if x < len(seen):
+ ans.append(seen[x])
+ else:
+ ans.append(-1)
+ x += 1
+ else:
+ x = 0
+ seen.insert(0, v)
+ #print(f'v => {v}, seen => {seen}, ans => {ans}, x = {x}')
+ return tuple(ans)
+
+import unittest
+
+class TestLv(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ (5, -1, -1): (5, -1),
+ (3, 7, -1, -1, -1): (7, 3, -1),
+ (2, -1, 4, -1, -1): (2, 4, 2),
+ (10, 20, -1, 30, -1, -1): (20, 30, 20),
+ (-1, -1, 5, -1): (-1, -1, 5),
+ }.items():
+ self.assertEqual(lv(inpt), otpt)
+
+unittest.main()