aboutsummaryrefslogtreecommitdiff
path: root/challenge-269
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-05-13 20:28:45 +1000
committerMichael Manring <michael@manring>2024-05-13 23:52:31 +1000
commitbc3467c3f4daeac99fd9a027a8e37529fae550f6 (patch)
tree6e345190b67a3a1b728982931f29f8e469d08e3e /challenge-269
parent02e309b3d451fff60404eb5ab3e539056f99ce0d (diff)
downloadperlweeklychallenge-club-bc3467c3f4daeac99fd9a027a8e37529fae550f6.tar.gz
perlweeklychallenge-club-bc3467c3f4daeac99fd9a027a8e37529fae550f6.tar.bz2
perlweeklychallenge-club-bc3467c3f4daeac99fd9a027a8e37529fae550f6.zip
pwc269 solution
Diffstat (limited to 'challenge-269')
-rw-r--r--challenge-269/pokgopun/go/ch-1.go77
-rw-r--r--challenge-269/pokgopun/go/ch-2.go146
-rw-r--r--challenge-269/pokgopun/python/ch-1.py63
-rw-r--r--challenge-269/pokgopun/python/ch-2.py134
4 files changed, 420 insertions, 0 deletions
diff --git a/challenge-269/pokgopun/go/ch-1.go b/challenge-269/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..634c7f1f13
--- /dev/null
+++ b/challenge-269/pokgopun/go/ch-1.go
@@ -0,0 +1,77 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-269/
+/*#
+
+Task 1: Bitwise OR
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of positive integers, @ints.
+
+ Write a script to find out if it is possible to select two or more
+ elements of the given array such that the bitwise OR of the selected
+ elements has atlest one trailing zero in its binary representation.
+
+Example 1
+
+Input: @ints = (1, 2, 3, 4, 5)
+Output: true
+
+Say, we pick 2 and 4, thier bitwise OR is 6. The binary representation of 6 is 1
+10.
+Return true since we have one trailing zero.
+
+Example 2
+
+Input: @ints = (2, 3, 8, 16)
+Output: true
+
+Say, we pick 2 and 8, thier bitwise OR is 10. The binary representation of 10 is
+ 1010.
+Return true since we have one trailing zero.
+
+Example 3
+
+Input: @ints = (1, 2, 5, 7, 9)
+Output: false
+
+Task 2: Distribute Elements
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (is ints) bwo() bool {
+ var c, i int
+ for _, i = range is {
+ if i%2 == 0 {
+ c++
+ }
+ if c > 1 {
+ return true
+ }
+ }
+ return false
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output bool
+ }{
+ {ints{1, 2, 3, 4, 5}, true},
+ {ints{2, 3, 8, 16}, true},
+ {ints{1, 2, 5, 7, 9}, false},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.bwo(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-269/pokgopun/go/ch-2.go b/challenge-269/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..65a9ae5072
--- /dev/null
+++ b/challenge-269/pokgopun/go/ch-2.go
@@ -0,0 +1,146 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-269/
+/*#
+
+Task 2: Distribute Elements
+
+Submitted by: [47]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of distinct integers, @ints.
+
+ Write a script to distribute the elements as described below:
+1) Put the 1st element of the given array to a new array @arr1.
+2) Put the 2nd element of the given array to a new array @arr2.
+
+ Once you have one element in each arrays, @arr1 and @arr2, then follow
+ the rule below:
+If the last element of the array @arr1 is greater than the last
+element of the array @arr2 then add the first element of the
+given array to @arr1 otherwise to the array @arr2.
+
+ When done distribution, return the concatenated arrays. @arr1 and
+ @arr2.
+
+Example 1
+
+Input: @ints = (2, 1, 3, 4, 5)
+Output: (2, 3, 4, 5, 1)
+
+1st operation:
+Add 1 to @arr1 = (2)
+
+2nd operation:
+Add 2 to @arr2 = (1)
+
+3rd operation:
+Now the last element of @arr1 is greater than the last element
+of @arr2, add 3 to @arr1 = (2, 3).
+
+4th operation:
+Again the last element of @arr1 is greate than the last element
+of @arr2, add 4 to @arr1 = (2, 3, 4)
+
+5th operation:
+Finally, the last element of @arr1 is again greater than the last
+element of @arr2, add 5 to @arr1 = (2, 3, 4, 5)
+
+Mow we have two arrays:
+@arr1 = (2, 3, 4, 5)
+@arr2 = (1)
+
+Concatenate the two arrays and return the final array: (2, 3, 4, 5, 1).
+
+Example 2
+
+Input: @ints = (3, 2, 4)
+Output: (3, 4, 2)
+
+1st operation:
+Add 1 to @arr1 = (3)
+
+2nd operation:
+Add 2 to @arr2 = (2)
+
+3rd operation:
+Now the last element of @arr1 is greater than the last element
+of @arr2, add 4 to @arr1 = (3, 4).
+
+Mow we have two arrays:
+@arr1 = (3, 4)
+@arr2 = (2)
+
+Concatenate the two arrays and return the final array: (3, 4, 2).
+
+Example 3
+
+Input: @ints = (5, 4, 3 ,8)
+Output: (5, 3, 4, 8)
+
+1st operation:
+Add 1 to @arr1 = (5)
+
+2nd operation:
+Add 2 to @arr2 = (4)
+
+3rd operation:
+Now the last element of @arr1 is greater than the last element
+of @arr2, add 3 to @arr1 = (5, 3).
+
+4th operation:
+Again the last element of @arr2 is greate than the last element
+of @arr1, add 8 to @arr2 = (4, 8)
+
+Mow we have two arrays:
+@arr1 = (5, 3)
+@arr2 = (4, 8)
+
+Concatenate the two arrays and return the final array: (5, 3, 4, 8).
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 19th May 2024.
+ __________________________________________________________________
+
+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 (is ints) distElem() ints {
+ l := len(is)
+ x, y, z := 0, 1, 2
+ var i int
+ for z < l {
+ if is[x] > is[y] {
+ i = is[z]
+ copy(is[z:], is[z+1:])
+ x++
+ copy(is[x+1:], is[x:])
+ is[x] = i
+ }
+ y++
+ z++
+ }
+ return is
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output ints
+ }{
+ {ints{2, 1, 3, 4, 5}, ints{2, 3, 4, 5, 1}},
+ {ints{3, 2, 4}, ints{3, 4, 2}},
+ {ints{5, 4, 3, 8}, ints{5, 3, 4, 8}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.distElem(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-269/pokgopun/python/ch-1.py b/challenge-269/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..18e3b5dc93
--- /dev/null
+++ b/challenge-269/pokgopun/python/ch-1.py
@@ -0,0 +1,63 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-269/
+"""
+
+Task 1: Bitwise OR
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of positive integers, @ints.
+
+ Write a script to find out if it is possible to select two or more
+ elements of the given array such that the bitwise OR of the selected
+ elements has atlest one trailing zero in its binary representation.
+
+Example 1
+
+Input: @ints = (1, 2, 3, 4, 5)
+Output: true
+
+Say, we pick 2 and 4, thier bitwise OR is 6. The binary representation of 6 is 1
+10.
+Return true since we have one trailing zero.
+
+Example 2
+
+Input: @ints = (2, 3, 8, 16)
+Output: true
+
+Say, we pick 2 and 8, thier bitwise OR is 10. The binary representation of 10 is
+ 1010.
+Return true since we have one trailing zero.
+
+Example 3
+
+Input: @ints = (1, 2, 5, 7, 9)
+Output: false
+
+Task 2: Distribute Elements
+"""
+### solution by pokgopun@gmail.com
+
+
+def bwo(ints: tuple):
+ c = 0
+ for i in ints:
+ if i % 2 == 0:
+ c += 1
+ if c > 1:
+ return True
+ return False
+
+import unittest
+
+class TestBwo(unittest.TestCase):
+ def test(self):
+ for ints, ans in {
+ (1, 2, 3, 4, 5): True,
+ (2, 3, 8, 16): True,
+ (1, 2, 5, 7, 9): False,
+ }.items():
+ self.assertEqual(bwo(ints),ans)
+
+unittest.main()
diff --git a/challenge-269/pokgopun/python/ch-2.py b/challenge-269/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..9850ef4fdd
--- /dev/null
+++ b/challenge-269/pokgopun/python/ch-2.py
@@ -0,0 +1,134 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-269/
+"""
+
+Task 2: Distribute Elements
+
+Submitted by: [47]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of distinct integers, @ints.
+
+ Write a script to distribute the elements as described below:
+1) Put the 1st element of the given array to a new array @arr1.
+2) Put the 2nd element of the given array to a new array @arr2.
+
+ Once you have one element in each arrays, @arr1 and @arr2, then follow
+ the rule below:
+If the last element of the array @arr1 is greater than the last
+element of the array @arr2 then add the first element of the
+given array to @arr1 otherwise to the array @arr2.
+
+ When done distribution, return the concatenated arrays. @arr1 and
+ @arr2.
+
+Example 1
+
+Input: @ints = (2, 1, 3, 4, 5)
+Output: (2, 3, 4, 5, 1)
+
+1st operation:
+Add 1 to @arr1 = (2)
+
+2nd operation:
+Add 2 to @arr2 = (1)
+
+3rd operation:
+Now the last element of @arr1 is greater than the last element
+of @arr2, add 3 to @arr1 = (2, 3).
+
+4th operation:
+Again the last element of @arr1 is greate than the last element
+of @arr2, add 4 to @arr1 = (2, 3, 4)
+
+5th operation:
+Finally, the last element of @arr1 is again greater than the last
+element of @arr2, add 5 to @arr1 = (2, 3, 4, 5)
+
+Mow we have two arrays:
+@arr1 = (2, 3, 4, 5)
+@arr2 = (1)
+
+Concatenate the two arrays and return the final array: (2, 3, 4, 5, 1).
+
+Example 2
+
+Input: @ints = (3, 2, 4)
+Output: (3, 4, 2)
+
+1st operation:
+Add 1 to @arr1 = (3)
+
+2nd operation:
+Add 2 to @arr2 = (2)
+
+3rd operation:
+Now the last element of @arr1 is greater than the last element
+of @arr2, add 4 to @arr1 = (3, 4).
+
+Mow we have two arrays:
+@arr1 = (3, 4)
+@arr2 = (2)
+
+Concatenate the two arrays and return the final array: (3, 4, 2).
+
+Example 3
+
+Input: @ints = (5, 4, 3 ,8)
+Output: (5, 3, 4, 8)
+
+1st operation:
+Add 1 to @arr1 = (5)
+
+2nd operation:
+Add 2 to @arr2 = (4)
+
+3rd operation:
+Now the last element of @arr1 is greater than the last element
+of @arr2, add 3 to @arr1 = (5, 3).
+
+4th operation:
+Again the last element of @arr2 is greate than the last element
+of @arr1, add 8 to @arr2 = (4, 8)
+
+Mow we have two arrays:
+@arr1 = (5, 3)
+@arr2 = (4, 8)
+
+Concatenate the two arrays and return the final array: (5, 3, 4, 8).
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 19th May 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def distElem(ints: tuple):
+ l = len(ints)
+ x,y,z = 0, 1, 2
+ ans = list(ints[:2])
+ while z < l:
+ #print(ints[x],"vs",ints[y])
+ if ans[x] > ans[y]:
+ x += 1
+ ans.insert(x,ints[z])
+ else:
+ ans.append(ints[z])
+ y += 1
+ z += 1
+ #print(ans)
+ return tuple(ans)
+
+import unittest
+
+class TestDistElem(unittest.TestCase):
+ def test(self):
+ for ints, ans in {
+ (2, 1, 3, 4, 5): (2, 3, 4, 5, 1),
+ (3, 2, 4): (3, 4, 2),
+ (5, 4, 3 ,8): (5, 3, 4, 8),
+ }.items():
+ self.assertEqual(distElem(ints),ans)
+
+unittest.main()