aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-07 14:43:42 +0000
committerGitHub <noreply@github.com>2023-11-07 14:43:42 +0000
commitaf894d900f03cd638f0295fd29073bc496be91a5 (patch)
tree67164eb442f5fe121aeab5be839d7d5cf0d1b1dd
parenta1c32551e2666568f5a07b681c077aa868c18e56 (diff)
parentb4afa897c67454d274607b8a367ba5d980e0136b (diff)
downloadperlweeklychallenge-club-af894d900f03cd638f0295fd29073bc496be91a5.tar.gz
perlweeklychallenge-club-af894d900f03cd638f0295fd29073bc496be91a5.tar.bz2
perlweeklychallenge-club-af894d900f03cd638f0295fd29073bc496be91a5.zip
Merge pull request #9020 from pokgopun/pwc242
pwc242 solution
-rw-r--r--challenge-242/pokgopun/go/ch-1.go76
-rw-r--r--challenge-242/pokgopun/go/ch-2.go88
-rw-r--r--challenge-242/pokgopun/python/ch-1.py55
-rw-r--r--challenge-242/pokgopun/python/ch-2.py57
4 files changed, 276 insertions, 0 deletions
diff --git a/challenge-242/pokgopun/go/ch-1.go b/challenge-242/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..697e9f4c06
--- /dev/null
+++ b/challenge-242/pokgopun/go/ch-1.go
@@ -0,0 +1,76 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-242/
+/*#
+
+Task 1: Missing Members
+
+Submitted by: [42]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given two arrays of integers.
+
+ Write a script to find out the missing members in each other arrays.
+
+Example 1
+
+Input: @arr1 = (1, 2, 3)
+ @arr2 = (2, 4, 6)
+Output: ([1, 3], [4, 6])
+
+(1, 2, 3) has 2 members (1, 3) missing in the array (2, 4, 6).
+(2, 4, 6) has 2 members (4, 6) missing in the array (1, 2, 3).
+
+Example 2
+
+Input: @arr1 = (1, 2, 3, 3)
+ @arr2 = (1, 1, 2, 2)
+Output: ([3])
+
+(1, 2, 3, 3) has 2 members (3, 3) missing in the array (1, 1, 2, 2). Since they
+are same, keep just one.
+(1, 1, 2, 2) has 0 member missing in the array (1, 2, 3, 3).
+
+Task 2: Flip Matrix
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "fmt"
+ "reflect"
+ "slices"
+)
+
+type arr []int
+
+type arrp struct {
+ arr1, arr2 arr
+}
+
+func (ap arrp) missing() (sa []arr) {
+ ap.arr1 = slices.Compact(ap.arr1)
+ ap.arr2 = slices.Compact(ap.arr2)
+ aa := [2]arr{ap.arr1, ap.arr2}
+ for i, v := range aa {
+ arr0 := make(arr, len(v))
+ copy(arr0, v)
+ arr0 = slices.DeleteFunc(arr0, func(n int) bool {
+ return slices.Contains(aa[i^1], n)
+ })
+ if len(arr0) > 0 {
+ sa = append(sa, arr0)
+ }
+ }
+ return sa
+}
+func main() {
+ for _, data := range []struct {
+ input arrp
+ output []arr
+ }{
+ {arrp{arr{1, 2, 3}, arr{2, 4, 6}}, []arr{arr{1, 3}, arr{4, 6}}},
+ {arrp{arr{1, 2, 3, 3}, arr{1, 1, 2, 2}}, []arr{arr{3}}},
+ } {
+ fmt.Println(reflect.DeepEqual(data.input.missing(), data.output))
+ }
+}
diff --git a/challenge-242/pokgopun/go/ch-2.go b/challenge-242/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..1ab3904423
--- /dev/null
+++ b/challenge-242/pokgopun/go/ch-2.go
@@ -0,0 +1,88 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-242/
+/*#
+
+Task 2: Flip Matrix
+
+Submitted by: [43]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given n x n binary matrix.
+
+ Write a script to flip the given matrix as below.
+1 1 0
+0 1 1
+0 0 1
+
+a) Reverse each row
+
+0 1 1
+1 1 0
+1 0 0
+
+b) Invert each member
+
+1 0 0
+0 0 1
+0 1 1
+
+Example 1
+
+Input: @matrix = ([1, 1, 0], [1, 0, 1], [0, 0, 0])
+Output: ([1, 0, 0], [0, 1, 0], [1, 1, 1])
+
+Example 2
+
+Input: @matrix = ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0])
+Output: ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0])
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 12th November
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "fmt"
+ "reflect"
+ "slices"
+)
+
+type row []int
+
+func (r row) invert() row {
+ for i := range r {
+ r[i] ^= 1
+ }
+ return r
+}
+
+func (r row) reverse() row {
+ slices.Reverse(r)
+ return r
+}
+
+type matrix []row
+
+func (m matrix) flip() matrix {
+ for _, v := range m {
+ v.invert().reverse()
+ }
+ return m
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output matrix
+ }{
+ {matrix{row{1, 1, 0}, row{1, 0, 1}, row{0, 0, 0}}, matrix{row{1, 0, 0}, row{0, 1, 0}, row{1, 1, 1}}},
+ {matrix{row{1, 1, 0, 0}, row{1, 0, 0, 1}, row{0, 1, 1, 1}, row{1, 0, 1, 0}}, matrix{row{1, 1, 0, 0}, row{0, 1, 1, 0}, row{0, 0, 0, 1}, row{1, 0, 1, 0}}},
+ } {
+ //fmt.Println(data.input, data.output)
+ fmt.Println(reflect.DeepEqual(data.input.flip(), data.output))
+ }
+}
diff --git a/challenge-242/pokgopun/python/ch-1.py b/challenge-242/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..bafb16347a
--- /dev/null
+++ b/challenge-242/pokgopun/python/ch-1.py
@@ -0,0 +1,55 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-242/
+"""
+
+Task 1: Missing Members
+
+Submitted by: [42]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given two arrays of integers.
+
+ Write a script to find out the missing members in each other arrays.
+
+Example 1
+
+Input: @arr1 = (1, 2, 3)
+ @arr2 = (2, 4, 6)
+Output: ([1, 3], [4, 6])
+
+(1, 2, 3) has 2 members (1, 3) missing in the array (2, 4, 6).
+(2, 4, 6) has 2 members (4, 6) missing in the array (1, 2, 3).
+
+Example 2
+
+Input: @arr1 = (1, 2, 3, 3)
+ @arr2 = (1, 1, 2, 2)
+Output: ([3])
+
+(1, 2, 3, 3) has 2 members (3, 3) missing in the array (1, 1, 2, 2). Since they
+are same, keep just one.
+(1, 1, 2, 2) has 0 member missing in the array (1, 2, 3, 3).
+
+Task 2: Flip Matrix
+"""
+### solution by pokgopun@gmail.com
+
+def findMissing(tup1, tup2):
+ return tuple(
+ filter(lambda x: len(x) > 0,
+ (
+ set(tup1).difference(set(tup2)),
+ set(tup2).difference(set(tup1)),
+ )
+ )
+ )
+
+
+
+for (inpt1,inpt2),otpt in {
+ ((1, 2, 3),(2, 4, 6)): ({1, 3}, {4, 6}),
+ ((1, 2, 3, 3),(1, 1, 2, 2)): ({3},),
+ }.items():
+ print(findMissing(inpt1,inpt2)==otpt)
+
+
+
diff --git a/challenge-242/pokgopun/python/ch-2.py b/challenge-242/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..71bacae9e1
--- /dev/null
+++ b/challenge-242/pokgopun/python/ch-2.py
@@ -0,0 +1,57 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-242/
+"""
+
+Task 2: Flip Matrix
+
+Submitted by: [43]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given n x n binary matrix.
+
+ Write a script to flip the given matrix as below.
+1 1 0
+0 1 1
+0 0 1
+
+a) Reverse each row
+
+0 1 1
+1 1 0
+1 0 0
+
+b) Invert each member
+
+1 0 0
+0 0 1
+0 1 1
+
+Example 1
+
+Input: @matrix = ([1, 1, 0], [1, 0, 1], [0, 0, 0])
+Output: ([1, 0, 0], [0, 1, 0], [1, 1, 1])
+
+Example 2
+
+Input: @matrix = ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0])
+Output: ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0])
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 12th November
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def flipMatrix(tup):
+ return tuple(
+ map(lambda x: tuple( i ^ 1 for i in reversed(x)) , tup)
+ )
+
+
+for inpt,otpt in {
+ ((1, 1, 0), (1, 0, 1), (0, 0, 0)): ((1, 0, 0), (0, 1, 0), (1, 1, 1)),
+ ((1, 1, 0, 0), (1, 0, 0, 1), (0, 1, 1, 1), (1, 0, 1, 0)): ((1, 1, 0, 0), (0, 1, 1, 0), (0, 0, 0, 1), (1, 0, 1, 0)),
+ }.items():
+ print(flipMatrix(inpt)==otpt)