aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-01-28 10:37:23 +0000
committerGitHub <noreply@github.com>2025-01-28 10:37:23 +0000
commitd7a9c5e6c01d08f2688eed39e20446ddc8c5d5e6 (patch)
tree2aaa31419503a609def62cd634b325790a1098ad
parentf3cc2f11227959de9daca2f27c96fb522f6941cb (diff)
parentdee32c6269b93973ec40023bd148ee8df1560cb2 (diff)
downloadperlweeklychallenge-club-d7a9c5e6c01d08f2688eed39e20446ddc8c5d5e6.tar.gz
perlweeklychallenge-club-d7a9c5e6c01d08f2688eed39e20446ddc8c5d5e6.tar.bz2
perlweeklychallenge-club-d7a9c5e6c01d08f2688eed39e20446ddc8c5d5e6.zip
Merge pull request #11504 from pokgopun/pwc306
Pwc306
-rw-r--r--challenge-306/pokgopun/go/ch-1.go80
-rw-r--r--challenge-306/pokgopun/go/ch-2.go95
-rw-r--r--challenge-306/pokgopun/python/ch-1.py66
-rw-r--r--challenge-306/pokgopun/python/ch-2.py82
4 files changed, 323 insertions, 0 deletions
diff --git a/challenge-306/pokgopun/go/ch-1.go b/challenge-306/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..c97fa86533
--- /dev/null
+++ b/challenge-306/pokgopun/go/ch-1.go
@@ -0,0 +1,80 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-306/
+/*#
+
+Task 1: Odd Sum
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of positive integers, @ints.
+
+ Write a script to return the sum of all possible odd-length subarrays
+ of the given array. A subarray is a contiguous subsequence of the
+ array.
+
+Example 1
+
+Input: @ints = (2, 5, 3, 6, 4)
+Output: 77
+
+Odd length sub-arrays:
+(2) => 2
+(5) => 5
+(3) => 3
+(6) => 6
+(4) => 4
+(2, 5, 3) => 10
+(5, 3, 6) => 14
+(3, 6, 4) => 13
+(2, 5, 3, 6, 4) => 20
+
+Sum => 2 + 5 + 3 + 6 + 4 + 10 + 14 + 13 + 20 => 77
+
+Example 2
+
+Input: @ints = (1, 3)
+Output: 4
+
+Task 2: Last Element
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (is ints) odsm() int {
+ sm := 0
+ l := len(is)
+ o := 1
+ for o <= l {
+ i := 0
+ for i+o <= l {
+ for _, v := range is[i : i+o] {
+ sm += v
+ }
+ i++
+ }
+ o += 2
+ }
+ return sm
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output int
+ }{
+ {ints{2, 5, 3, 6, 4}, 77},
+ {ints{1, 3}, 4},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.odsm(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-306/pokgopun/go/ch-2.go b/challenge-306/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..dce30eb496
--- /dev/null
+++ b/challenge-306/pokgopun/go/ch-2.go
@@ -0,0 +1,95 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-306/
+/*#
+
+Task 2: Last Element
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a array of integers, @ints.
+
+ Write a script to play a game where you pick two biggest integers in
+ the given array, say x and y. Then do the following:
+a) if x == y then remove both from the given array
+b) if x != y then remove x and replace y with (y - x)
+
+ At the end of the game, there is at most one element left.
+
+ Return the last element if found otherwise return 0.
+
+Example 1
+
+Input: @ints = (3, 8, 5, 2, 9, 2)
+Output: 1
+
+Step 1: pick 8 and 9 => (3, 5, 2, 1, 2)
+Step 2: pick 3 and 5 => (2, 2, 1, 2)
+Step 3: pick 2 and 1 => (1, 2, 2)
+Step 4: pick 2 and 1 => (1, 2)
+Step 5: pick 1 and 2 => (1)
+
+Example 2
+
+Input: @ints = (3, 2, 5)
+Output: 0
+
+Step 1: pick 3 and 5 => (2, 2)
+Step 2: pick 2 and 2 => ()
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 2^nd January
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "slices"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (is ints) lstelm() int {
+ for {
+ l := len(is)
+ switch l {
+ case 0:
+ return 0
+ case 1:
+ return is[0]
+ case 2:
+ default:
+ slices.Sort(is)
+ }
+ d := is[l-2] - is[l-1]
+ if d == 0 {
+ is = is[:l-2]
+ continue
+ }
+ if d < 0 {
+ d *= -1
+ }
+ is[l-2] = d
+ is = is[:l-1]
+ }
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output int
+ }{
+ {ints{3, 8, 5, 2, 9, 2}, 1},
+ {ints{3, 2, 5}, 0},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.lstelm(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-306/pokgopun/python/ch-1.py b/challenge-306/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..545c866cd8
--- /dev/null
+++ b/challenge-306/pokgopun/python/ch-1.py
@@ -0,0 +1,66 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-306/
+"""
+
+Task 1: Odd Sum
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of positive integers, @ints.
+
+ Write a script to return the sum of all possible odd-length subarrays
+ of the given array. A subarray is a contiguous subsequence of the
+ array.
+
+Example 1
+
+Input: @ints = (2, 5, 3, 6, 4)
+Output: 77
+
+Odd length sub-arrays:
+(2) => 2
+(5) => 5
+(3) => 3
+(6) => 6
+(4) => 4
+(2, 5, 3) => 10
+(5, 3, 6) => 14
+(3, 6, 4) => 13
+(2, 5, 3, 6, 4) => 20
+
+Sum => 2 + 5 + 3 + 6 + 4 + 10 + 14 + 13 + 20 => 77
+
+Example 2
+
+Input: @ints = (1, 3)
+Output: 4
+
+Task 2: Last Element
+"""
+### solution by pokgopun@gmail.com
+
+from typing import Tuple
+
+def odsm(ints: Tuple[int])-> int:
+ sm = 0
+ l = len(ints)
+ for o in range(1, l+1, 2):
+ i = 0
+ while i + o <= l:
+ sm += sum(ints[i:i+o])
+ i += 1
+ return sm
+
+import unittest
+
+class TestOdsm(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ (2, 5, 3, 6, 4): 77,
+ (1, 3): 4,
+ }.items():
+ self.assertEqual(odsm(inpt),otpt)
+
+unittest.main()
+
+
diff --git a/challenge-306/pokgopun/python/ch-2.py b/challenge-306/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..f6d8d9f298
--- /dev/null
+++ b/challenge-306/pokgopun/python/ch-2.py
@@ -0,0 +1,82 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-306/
+"""
+
+Task 2: Last Element
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a array of integers, @ints.
+
+ Write a script to play a game where you pick two biggest integers in
+ the given array, say x and y. Then do the following:
+a) if x == y then remove both from the given array
+b) if x != y then remove x and replace y with (y - x)
+
+ At the end of the game, there is at most one element left.
+
+ Return the last element if found otherwise return 0.
+
+Example 1
+
+Input: @ints = (3, 8, 5, 2, 9, 2)
+Output: 1
+
+Step 1: pick 8 and 9 => (3, 5, 2, 1, 2)
+Step 2: pick 3 and 5 => (2, 2, 1, 2)
+Step 3: pick 2 and 1 => (1, 2, 2)
+Step 4: pick 2 and 1 => (1, 2)
+Step 5: pick 1 and 2 => (1)
+
+Example 2
+
+Input: @ints = (3, 2, 5)
+Output: 0
+
+Step 1: pick 3 and 5 => (2, 2)
+Step 2: pick 2 and 2 => ()
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 2^nd January
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+from typing import Tuple
+
+def lstelm(ints: Tuple[int]) -> int:
+ lst = list(ints)
+ while True:
+ l = len(lst)
+ match l:
+ case 0:
+ return 0
+ case 1:
+ return lst[0]
+ case 2:
+ pass
+ case _:
+ lst.sort()
+ d = lst[-2] - lst[-1]
+ if d == 0:
+ lst = lst[:-2]
+ continue
+ if d < 0:
+ d *= -1
+ lst[-2] = d
+ lst = lst[:-1]
+
+import unittest
+
+class TestLstelm(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ (3, 8, 5, 2, 9, 2): 1,
+ (3, 2, 5): 0,
+ }.items():
+ self.assertEqual(lstelm(inpt), otpt)
+
+unittest.main()