aboutsummaryrefslogtreecommitdiff
path: root/challenge-324
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-06-03 13:07:26 +0100
committerGitHub <noreply@github.com>2025-06-03 13:07:26 +0100
commit5ece8332976881bb02efcf826d73afaf939682bd (patch)
tree9ac895a1c60a7b448652cd870d5eded6e136b1f8 /challenge-324
parent1de847b1c005c861d46f2cee60d931a416741117 (diff)
parent3bea8b0e3ce345128c749e991793b8ef6b9082a4 (diff)
downloadperlweeklychallenge-club-5ece8332976881bb02efcf826d73afaf939682bd.tar.gz
perlweeklychallenge-club-5ece8332976881bb02efcf826d73afaf939682bd.tar.bz2
perlweeklychallenge-club-5ece8332976881bb02efcf826d73afaf939682bd.zip
Merge pull request #12119 from pokgopun/pwc324
Pwc324
Diffstat (limited to 'challenge-324')
-rw-r--r--challenge-324/pokgopun/go/ch-1.go68
-rw-r--r--challenge-324/pokgopun/go/ch-2.go136
-rw-r--r--challenge-324/pokgopun/python/ch-1.py47
-rw-r--r--challenge-324/pokgopun/python/ch-2.py76
4 files changed, 327 insertions, 0 deletions
diff --git a/challenge-324/pokgopun/go/ch-1.go b/challenge-324/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..9163d8a8ca
--- /dev/null
+++ b/challenge-324/pokgopun/go/ch-1.go
@@ -0,0 +1,68 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-324/
+/*#
+
+Task 1: 2D Array
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers and two integers $r amd $c.
+
+ Write a script to create two dimension array having $r rows and $c
+ columns using the given array.
+
+Example 1
+
+Input: @ints = (1, 2, 3, 4), $r = 2, $c = 2
+Output: ([1, 2], [3, 4])
+
+Example 2
+
+Input: @ints = (1, 2, 3), $r = 1, $c = 3
+Output: ([1, 2, 3])
+
+Example 3
+
+Input: @ints = (1, 2, 3, 4), $r = 4, $c = 1
+Output: ([1], [2], [3], [4])
+
+Task 2: Total XOR
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+type input struct {
+ ints ints
+ r, c int
+}
+
+func (in input) process() []ints {
+ var r []ints
+ for i := range in.r {
+ r = append(r, in.ints[i*in.c:i*in.c+in.c])
+ }
+ return r
+}
+
+func main() {
+ for _, data := range []struct {
+ input input
+ output []ints
+ }{
+ {input{ints{1, 2, 3, 4}, 2, 2}, []ints{ints{1, 2}, ints{3, 4}}},
+ {input{ints{1, 2, 3}, 1, 3}, []ints{ints{1, 2, 3}}},
+ {input{ints{1, 2, 3, 4}, 4, 1}, []ints{ints{1}, ints{2}, ints{3}, ints{4}}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-324/pokgopun/go/ch-2.go b/challenge-324/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..7e4bf693dc
--- /dev/null
+++ b/challenge-324/pokgopun/go/ch-2.go
@@ -0,0 +1,136 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-324/
+/*#
+
+Task 2: Total XOR
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to return the sum of total XOR for every subset of given
+ array.
+
+Example 1
+
+Input: @ints = (1, 3)
+Output: 6
+
+Subset [1], total XOR = 1
+Subset [3], total XOR = 3
+Subset [1, 3], total XOR => 1 XOR 3 => 2
+
+Sum of total XOR => 1 + 3 + 2 => 6
+
+Example 2
+
+Input: @ints = (5, 1, 6)
+Output: 28
+
+Subset [5], total XOR = 5
+Subset [1], total XOR = 1
+Subset [6], total XOR = 6
+Subset [5, 1], total XOR => 5 XOR 1 => 4
+Subset [5, 6], total XOR => 5 XOR 6 => 3
+Subset [1, 6], total XOR => 1 XOR 6 => 7
+Subset [5, 1, 6], total XOR => 5 XOR 1 XOR 6 => 2
+
+Sum of total XOR => 5 + 1 + 6 + 4 + 3 + 7 + 2 => 28
+
+Example 3
+
+Input: @ints = (3, 4, 5, 6, 7, 8)
+Output: 480
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 8th June 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "iter"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (in ints) process() int {
+ var tx, x int
+ for r := range len(in) {
+ for cmb := range Combinations(in, uint(r+1)) {
+ x = cmb[0]
+ for _, v := range cmb[1:] {
+ x ^= v
+ }
+ tx += x
+ }
+ }
+ return tx
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output int
+ }{
+ {ints{1, 3}, 6},
+ {ints{5, 1, 6}, 28},
+ {ints{3, 4, 5, 6, 7, 8}, 480},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
+
+// transcribed from https://docs.python.org/3/library/itertools.html#itertools.combinations
+func Combinations[E any](s []E, r uint) iter.Seq[[]E] {
+ return func(yield func([]E) bool) {
+ n := uint(len(s))
+ if r == 0 || r > n {
+ return
+ }
+ result := make([]E, r)
+ copy(result, s)
+ if !yield(result) {
+ return
+ }
+ idx := make([]uint, r)
+ for i := range r {
+ idx[i] = uint(i)
+ }
+ var i, j uint
+ for {
+ i = r
+ for {
+ i--
+ if idx[i] != i+n-r {
+ break
+ }
+ if i > 0 {
+ continue
+ }
+ return
+ }
+ idx[i]++
+ j = i + 1
+ for j < r {
+ idx[j] = idx[j-1] + 1
+ j++
+ }
+ result := make([]E, r)
+ for i, v := range idx {
+ result[i] = s[v]
+ }
+ if !yield(result) {
+ return
+ }
+ }
+ }
+}
diff --git a/challenge-324/pokgopun/python/ch-1.py b/challenge-324/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..d4f0a3057d
--- /dev/null
+++ b/challenge-324/pokgopun/python/ch-1.py
@@ -0,0 +1,47 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-324/
+"""
+
+Task 1: 2D Array
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers and two integers $r amd $c.
+
+ Write a script to create two dimension array having $r rows and $c
+ columns using the given array.
+
+Example 1
+
+Input: @ints = (1, 2, 3, 4), $r = 2, $c = 2
+Output: ([1, 2], [3, 4])
+
+Example 2
+
+Input: @ints = (1, 2, 3), $r = 1, $c = 3
+Output: ([1, 2, 3])
+
+Example 3
+
+Input: @ints = (1, 2, 3, 4), $r = 4, $c = 1
+Output: ([1], [2], [3], [4])
+
+Task 2: Total XOR
+"""
+### solution by pokgopun@gmail.com
+
+def tda(ints: tuple[int], r: int, c: int) -> tuple[list[int]]:
+ return tuple(list(ints[i*c:i*c+c]) for i in range(r))
+
+import unittest
+
+class TestTda(unittest.TestCase):
+ def test(self):
+ for (ints, r, c), otpt in {
+ ((1, 2, 3, 4), 2, 2): ([1, 2], [3, 4]),
+ ((1, 2, 3), 1, 3): ([1, 2, 3],),
+ ((1, 2, 3, 4), 4, 1): ([1], [2], [3], [4]),
+ }.items():
+ self.assertEqual(tda(ints,r,c), otpt)
+
+unittest.main()
diff --git a/challenge-324/pokgopun/python/ch-2.py b/challenge-324/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..96e81c7966
--- /dev/null
+++ b/challenge-324/pokgopun/python/ch-2.py
@@ -0,0 +1,76 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-324/
+"""
+
+Task 2: Total XOR
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to return the sum of total XOR for every subset of given
+ array.
+
+Example 1
+
+Input: @ints = (1, 3)
+Output: 6
+
+Subset [1], total XOR = 1
+Subset [3], total XOR = 3
+Subset [1, 3], total XOR => 1 XOR 3 => 2
+
+Sum of total XOR => 1 + 3 + 2 => 6
+
+Example 2
+
+Input: @ints = (5, 1, 6)
+Output: 28
+
+Subset [5], total XOR = 5
+Subset [1], total XOR = 1
+Subset [6], total XOR = 6
+Subset [5, 1], total XOR => 5 XOR 1 => 4
+Subset [5, 6], total XOR => 5 XOR 6 => 3
+Subset [1, 6], total XOR => 1 XOR 6 => 7
+Subset [5, 1, 6], total XOR => 5 XOR 1 XOR 6 => 2
+
+Sum of total XOR => 5 + 1 + 6 + 4 + 3 + 7 + 2 => 28
+
+Example 3
+
+Input: @ints = (3, 4, 5, 6, 7, 8)
+Output: 480
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 8th June 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+from itertools import combinations
+
+def txor(ints: tuple[int]) -> int:
+ tx = 0
+ for r in range(len(ints)):
+ for cmb in combinations(ints,r+1):
+ x = cmb[0]
+ for n in cmb[1:]:
+ x ^= n
+ tx += x
+ return tx
+
+import unittest
+
+class TestTxor(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ (1, 3): 6,
+ (5, 1, 6): 28,
+ (3, 4, 5, 6, 7, 8): 480,
+ }.items():
+ self.assertEqual(txor(inpt), otpt)
+
+unittest.main()