aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-09-08 17:56:46 +0100
committerGitHub <noreply@github.com>2025-09-08 17:56:46 +0100
commit6cc30286d62c798a5feddb5453c87fa1d0578ef8 (patch)
tree17aa7eff011f7af3837a41fd45e897d593460a8d
parent02465027ca8861108174aaa26eef9e23b90cf693 (diff)
parent5acea8ff477a10b87842a04a67a1dbd0b16170e1 (diff)
downloadperlweeklychallenge-club-6cc30286d62c798a5feddb5453c87fa1d0578ef8.tar.gz
perlweeklychallenge-club-6cc30286d62c798a5feddb5453c87fa1d0578ef8.tar.bz2
perlweeklychallenge-club-6cc30286d62c798a5feddb5453c87fa1d0578ef8.zip
Merge pull request #12648 from pokgopun/pwc338
Pwc338
-rw-r--r--challenge-338/pokgopun/go/ch-1.go108
-rw-r--r--challenge-338/pokgopun/go/ch-2.go230
-rw-r--r--challenge-338/pokgopun/python/ch-1.py83
-rw-r--r--challenge-338/pokgopun/python/ch-2.py206
4 files changed, 627 insertions, 0 deletions
diff --git a/challenge-338/pokgopun/go/ch-1.go b/challenge-338/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..210e1b83a8
--- /dev/null
+++ b/challenge-338/pokgopun/go/ch-1.go
@@ -0,0 +1,108 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-338/
+/*#
+
+Task 1: Highest Row
+
+Submitted by: [44]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a m x n matrix.
+
+ Write a script to find the highest row sum in the given matrix.
+
+Example 1
+
+Input: @matrix = ([4, 4, 4, 4],
+ [10, 0, 0, 0],
+ [2, 2, 2, 9])
+Output: 16
+
+Row 1: 4 + 4 + 4 + 4 => 16
+Row 2: 10 + 0 + 0 + 0 => 10
+Row 3: 2 + 2 + 2 + 9 => 15
+
+Example 2
+
+Input: @matrix = ([1, 5],
+ [7, 3],
+ [3, 5])
+Output: 10
+
+Example 3
+
+Input: @matrix = ([1, 2, 3],
+ [3, 2, 1])
+Output: 6
+
+Example 4
+
+Input: @matrix = ([2, 8, 7],
+ [7, 1, 3],
+ [1, 9, 5])
+Output: 17
+
+Example 5
+
+Input: @matrix = ([10, 20, 30],
+ [5, 5, 5],
+ [0, 100, 0],
+ [25, 25, 25])
+Output: 100
+
+Task 2: Max Distance
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (in ints) sum() int {
+ s := 0
+ for _, v := range in {
+ s += v
+ }
+ return s
+}
+
+type input []ints
+
+func (in input) process() int {
+ s := in[0].sum()
+ for _, v := range in[1:] {
+ s = max(s, v.sum())
+ }
+ return s
+}
+
+func main() {
+ for _, data := range []struct {
+ input input
+ output int
+ }{
+ {input{ints{4, 4, 4, 4},
+ ints{10, 0, 0, 0},
+ ints{2, 2, 2, 9}}, 16},
+ {input{ints{1, 5},
+ ints{7, 3},
+ ints{3, 5}}, 10},
+ {input{ints{1, 2, 3},
+ ints{3, 2, 1}}, 6},
+ {input{ints{2, 8, 7},
+ ints{7, 1, 3},
+ ints{1, 9, 5}}, 17},
+ {input{ints{10, 20, 30},
+ ints{5, 5, 5},
+ ints{0, 100, 0},
+ ints{25, 25, 25}}, 100},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-338/pokgopun/go/ch-2.go b/challenge-338/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..ba921a2424
--- /dev/null
+++ b/challenge-338/pokgopun/go/ch-2.go
@@ -0,0 +1,230 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-338/
+/*#
+
+Task 2: Max Distance
+
+Submitted by: [45]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given two integer arrays, @arr1 and @arr2.
+
+ Write a script to find the maximum difference between any pair of
+ values from both arrays.
+
+Example 1
+
+Input: @arr1 = (4, 5, 7)
+ @arr2 = (9, 1, 3, 4)
+Output: 6
+
+With element $arr1[0] = 4
+| 4 - 9 | = 5
+| 4 - 1 | = 3
+| 4 - 3 | = 1
+| 4 - 4 | = 0
+max distance = 5
+
+With element $arr1[1] = 5
+| 5 - 9 | = 4
+| 5 - 1 | = 4
+| 5 - 3 | = 2
+| 5 - 4 | = 1
+max distance = 4
+
+With element $arr1[2] = 7
+| 7 - 9 | = 2
+| 7 - 1 | = 6
+| 7 - 3 | = 4
+| 7 - 4 | = 4
+max distance = 6
+
+max (5, 6, 6) = 6
+
+Example 2
+
+Input: @arr1 = (2, 3, 5, 4)
+ @arr2 = (3, 2, 5, 5, 8, 7)
+Output: 6
+
+With element $arr1[0] = 2
+| 2 - 3 | = 1
+| 2 - 2 | = 0
+| 2 - 5 | = 3
+| 2 - 5 | = 3
+| 2 - 8 | = 6
+| 2 - 7 | = 5
+max distance = 6
+
+With element $arr1[1] = 3
+| 3 - 3 | = 0
+| 3 - 2 | = 1
+| 3 - 5 | = 2
+| 3 - 5 | = 2
+| 3 - 8 | = 5
+| 3 - 7 | = 4
+max distance = 5
+
+With element $arr1[2] = 5
+| 5 - 3 | = 2
+| 5 - 2 | = 3
+| 5 - 5 | = 0
+| 5 - 5 | = 0
+| 5 - 8 | = 3
+| 5 - 7 | = 2
+max distance = 3
+
+With element $arr1[3] = 4
+| 4 - 3 | = 1
+| 4 - 2 | = 2
+| 4 - 5 | = 1
+| 4 - 5 | = 1
+| 4 - 8 | = 4
+| 4 - 7 | = 3
+max distance = 4
+
+max (5, 6, 3, 4) = 6
+
+Example 3
+
+Input: @arr1 = (2, 1, 11, 3)
+ @arr2 = (2, 5, 10, 2)
+Output: 9
+
+With element $arr1[0] = 2
+| 2 - 2 | = 0
+| 2 - 5 | = 3
+| 2 - 10 | = 8
+| 2 - 2 | = 0
+max distance = 8
+
+With element $arr1[1] = 1
+| 1 - 2 | = 1
+| 1 - 5 | = 4
+| 1 - 10 | = 9
+| 1 - 2 | = 1
+max distance = 9
+
+With element $arr1[2] = 11
+| 11 - 2 | = 9
+| 11 - 5 | = 6
+| 11 - 10 | = 1
+| 11 - 2 | = 9
+max distance = 9
+
+With element $arr1[3] = 3
+| 3 - 2 | = 1
+| 3 - 5 | = 2
+| 3 - 10 | = 7
+| 3 - 2 | = 1
+max distance = 7
+
+max (8, 9, 9, 7) = 9
+
+Example 4
+
+Input: @arr1 = (1, 2, 3)
+ @arr2 = (3, 2, 1)
+Output: 2
+
+With element $arr1[0] = 1
+| 1 - 3 | = 2
+| 1 - 2 | = 1
+| 1 - 1 | = 0
+max distance = 2
+
+With element $arr1[1] = 2
+| 2 - 3 | = 1
+| 2 - 2 | = 0
+| 2 - 1 | = 1
+max distance = 1
+
+With element $arr1[2] = 3
+| 3 - 3 | = 0
+| 3 - 2 | = 1
+| 3 - 1 | = 2
+max distance = 2
+
+max (2, 1, 2) = 2
+
+Example 5
+
+Input: @arr1 = (1, 0, 2, 3)
+ @arr2 = (5, 0)
+Output: 5
+
+With element $arr1[0] = 1
+| 1 - 5 | = 4
+| 1 - 0 | = 1
+max distance = 4
+
+With element $arr1[1] = 0
+| 0 - 5 | = 5
+| 0 - 0 | = 0
+max distance = 5
+
+With element $arr1[2] = 2
+| 2 - 5 | = 3
+| 2 - 0 | = 2
+max distance = 3
+
+With element $arr1[3] = 3
+| 3 - 5 | = 2
+| 3 - 0 | = 3
+max distance = 3
+
+max (4, 5, 3, 3) = 5
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 14th September
+ 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) md(n int) int {
+ mx := max(in[0], n) - min(in[0], n)
+ for _, v := range in[1:] {
+ mx = max(mx, max(v, n)-min(v, n))
+ }
+ return mx
+}
+
+type input struct {
+ arr1, arr2 ints
+}
+
+func (in input) process() int {
+ mx := in.arr2.md(in.arr1[0])
+ for _, v := range in.arr1[1:] {
+ mx = max(mx, in.arr2.md(v))
+ }
+ return mx
+}
+
+func main() {
+ for _, data := range []struct {
+ input input
+ output int
+ }{
+ {input{ints{4, 5, 7}, ints{9, 1, 3, 4}}, 6},
+ {input{ints{2, 3, 5, 4}, ints{3, 2, 5, 5, 8, 7}}, 6},
+ {input{ints{2, 1, 11, 3}, ints{2, 5, 10, 2}}, 9},
+ {input{ints{1, 2, 3}, ints{3, 2, 1}}, 2},
+ {input{ints{1, 0, 2, 3}, ints{5, 0}}, 5},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-338/pokgopun/python/ch-1.py b/challenge-338/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..4bebb2083e
--- /dev/null
+++ b/challenge-338/pokgopun/python/ch-1.py
@@ -0,0 +1,83 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-338/
+"""
+
+Task 1: Highest Row
+
+Submitted by: [44]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a m x n matrix.
+
+ Write a script to find the highest row sum in the given matrix.
+
+Example 1
+
+Input: @matrix = ([4, 4, 4, 4],
+ [10, 0, 0, 0],
+ [2, 2, 2, 9])
+Output: 16
+
+Row 1: 4 + 4 + 4 + 4 => 16
+Row 2: 10 + 0 + 0 + 0 => 10
+Row 3: 2 + 2 + 2 + 9 => 15
+
+Example 2
+
+Input: @matrix = ([1, 5],
+ [7, 3],
+ [3, 5])
+Output: 10
+
+Example 3
+
+Input: @matrix = ([1, 2, 3],
+ [3, 2, 1])
+Output: 6
+
+Example 4
+
+Input: @matrix = ([2, 8, 7],
+ [7, 1, 3],
+ [1, 9, 5])
+Output: 17
+
+Example 5
+
+Input: @matrix = ([10, 20, 30],
+ [5, 5, 5],
+ [0, 100, 0],
+ [25, 25, 25])
+Output: 100
+
+Task 2: Max Distance
+"""
+### solution by pokgopun@gmail.com
+
+def hr(matrix: tuple[tuple[int]]) -> int:
+ return max(sum(e) for e in matrix)
+
+import unittest
+
+class TestHr(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ ((4, 4, 4, 4),
+ (10, 0, 0, 0),
+ (2, 2, 2, 9)): 16,
+ ((1, 5),
+ (7, 3),
+ (3, 5)): 10,
+ ((1, 2, 3),
+ (3, 2, 1)): 6,
+ ((2, 8, 7),
+ (7, 1, 3),
+ (1, 9, 5)): 17,
+ ((10, 20, 30),
+ (5, 5, 5),
+ (0, 100, 0),
+ (25, 25, 25)): 100,
+ }.items():
+ self.assertEqual(hr(inpt),otpt)
+
+unittest.main()
+
diff --git a/challenge-338/pokgopun/python/ch-2.py b/challenge-338/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..f33632ff42
--- /dev/null
+++ b/challenge-338/pokgopun/python/ch-2.py
@@ -0,0 +1,206 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-338/
+"""
+
+Task 2: Max Distance
+
+Submitted by: [45]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given two integer arrays, @arr1 and @arr2.
+
+ Write a script to find the maximum difference between any pair of
+ values from both arrays.
+
+Example 1
+
+Input: @arr1 = (4, 5, 7)
+ @arr2 = (9, 1, 3, 4)
+Output: 6
+
+With element $arr1[0] = 4
+| 4 - 9 | = 5
+| 4 - 1 | = 3
+| 4 - 3 | = 1
+| 4 - 4 | = 0
+max distance = 5
+
+With element $arr1[1] = 5
+| 5 - 9 | = 4
+| 5 - 1 | = 4
+| 5 - 3 | = 2
+| 5 - 4 | = 1
+max distance = 4
+
+With element $arr1[2] = 7
+| 7 - 9 | = 2
+| 7 - 1 | = 6
+| 7 - 3 | = 4
+| 7 - 4 | = 4
+max distance = 6
+
+max (5, 6, 6) = 6
+
+Example 2
+
+Input: @arr1 = (2, 3, 5, 4)
+ @arr2 = (3, 2, 5, 5, 8, 7)
+Output: 6
+
+With element $arr1[0] = 2
+| 2 - 3 | = 1
+| 2 - 2 | = 0
+| 2 - 5 | = 3
+| 2 - 5 | = 3
+| 2 - 8 | = 6
+| 2 - 7 | = 5
+max distance = 6
+
+With element $arr1[1] = 3
+| 3 - 3 | = 0
+| 3 - 2 | = 1
+| 3 - 5 | = 2
+| 3 - 5 | = 2
+| 3 - 8 | = 5
+| 3 - 7 | = 4
+max distance = 5
+
+With element $arr1[2] = 5
+| 5 - 3 | = 2
+| 5 - 2 | = 3
+| 5 - 5 | = 0
+| 5 - 5 | = 0
+| 5 - 8 | = 3
+| 5 - 7 | = 2
+max distance = 3
+
+With element $arr1[3] = 4
+| 4 - 3 | = 1
+| 4 - 2 | = 2
+| 4 - 5 | = 1
+| 4 - 5 | = 1
+| 4 - 8 | = 4
+| 4 - 7 | = 3
+max distance = 4
+
+max (5, 6, 3, 4) = 6
+
+Example 3
+
+Input: @arr1 = (2, 1, 11, 3)
+ @arr2 = (2, 5, 10, 2)
+Output: 9
+
+With element $arr1[0] = 2
+| 2 - 2 | = 0
+| 2 - 5 | = 3
+| 2 - 10 | = 8
+| 2 - 2 | = 0
+max distance = 8
+
+With element $arr1[1] = 1
+| 1 - 2 | = 1
+| 1 - 5 | = 4
+| 1 - 10 | = 9
+| 1 - 2 | = 1
+max distance = 9
+
+With element $arr1[2] = 11
+| 11 - 2 | = 9
+| 11 - 5 | = 6
+| 11 - 10 | = 1
+| 11 - 2 | = 9
+max distance = 9
+
+With element $arr1[3] = 3
+| 3 - 2 | = 1
+| 3 - 5 | = 2
+| 3 - 10 | = 7
+| 3 - 2 | = 1
+max distance = 7
+
+max (8, 9, 9, 7) = 9
+
+Example 4
+
+Input: @arr1 = (1, 2, 3)
+ @arr2 = (3, 2, 1)
+Output: 2
+
+With element $arr1[0] = 1
+| 1 - 3 | = 2
+| 1 - 2 | = 1
+| 1 - 1 | = 0
+max distance = 2
+
+With element $arr1[1] = 2
+| 2 - 3 | = 1
+| 2 - 2 | = 0
+| 2 - 1 | = 1
+max distance = 1
+
+With element $arr1[2] = 3
+| 3 - 3 | = 0
+| 3 - 2 | = 1
+| 3 - 1 | = 2
+max distance = 2
+
+max (2, 1, 2) = 2
+
+Example 5
+
+Input: @arr1 = (1, 0, 2, 3)
+ @arr2 = (5, 0)
+Output: 5
+
+With element $arr1[0] = 1
+| 1 - 5 | = 4
+| 1 - 0 | = 1
+max distance = 4
+
+With element $arr1[1] = 0
+| 0 - 5 | = 5
+| 0 - 0 | = 0
+max distance = 5
+
+With element $arr1[2] = 2
+| 2 - 5 | = 3
+| 2 - 0 | = 2
+max distance = 3
+
+With element $arr1[3] = 3
+| 3 - 5 | = 2
+| 3 - 0 | = 3
+max distance = 3
+
+max (4, 5, 3, 3) = 5
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 14th September
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def md(arr1: tuple[int], arr2: tuple[int]) -> int:
+ return max(
+ max(e) for e in (
+ (abs(e1 - e2) for e2 in arr2) for e1 in arr1
+ )
+ )
+
+import unittest
+
+class TestMd(unittest.TestCase):
+ def test(self):
+ for (arr1,arr2), otpt in {
+ ((4, 5, 7), (9, 1, 3, 4)): 6,
+ ((2, 3, 5, 4), (3, 2, 5, 5, 8, 7)): 6,
+ ((2, 1, 11, 3), (2, 5, 10, 2)): 9,
+ ((1, 2, 3), (3, 2, 1)): 2,
+ ((1, 0, 2, 3), (5, 0)): 5,
+ }.items():
+ self.assertEqual(md(arr1,arr2),otpt)
+
+unittest.main()