aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-08-20 17:24:42 +0100
committerGitHub <noreply@github.com>2024-08-20 17:24:42 +0100
commitf9ccc7e49e7cf61ab9159d49ba4d4f8249cda78e (patch)
treefb7356aeef77fb319135614ab9059523c4bb1162
parent27c083fc776b5eb98d6d4696e67b2fb3b9effffa (diff)
parent6328c17e03d860bc27d3f87c086b16866811a59f (diff)
downloadperlweeklychallenge-club-f9ccc7e49e7cf61ab9159d49ba4d4f8249cda78e.tar.gz
perlweeklychallenge-club-f9ccc7e49e7cf61ab9159d49ba4d4f8249cda78e.tar.bz2
perlweeklychallenge-club-f9ccc7e49e7cf61ab9159d49ba4d4f8249cda78e.zip
Merge pull request #10673 from pokgopun/pwc283
Pwc283
-rw-r--r--challenge-283/pokgopun/go/ch-1.go79
-rw-r--r--challenge-283/pokgopun/go/ch-2.go78
-rw-r--r--challenge-283/pokgopun/python/ch-1.py55
-rw-r--r--challenge-283/pokgopun/python/ch-2.py57
4 files changed, 269 insertions, 0 deletions
diff --git a/challenge-283/pokgopun/go/ch-1.go b/challenge-283/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..9094f1f419
--- /dev/null
+++ b/challenge-283/pokgopun/go/ch-1.go
@@ -0,0 +1,79 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-283/
+/*#
+
+Task 1: Unique Number
+
+Submitted by: [45]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers, @ints, where every elements appears
+ more than once except one element.
+
+ Write a script to find the one element that appears exactly one time.
+
+Example 1
+
+Input: @ints = (3, 3, 1)
+Output: 1
+
+Example 2
+
+Input: @ints = (3, 2, 4, 2, 4)
+Output: 3
+
+Example 3
+
+Input: @ints = (1)
+Output: 1
+
+Example 4
+
+Input: @ints = (4, 3, 1, 1, 1, 4)
+Output: 3
+
+Task 2: Digit Count Value
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (is ints) uniqNum() int {
+ m := make(map[int]bool)
+ for _, v := range is {
+ _, ok := m[v]
+ if ok {
+ m[v] = false
+ } else {
+ m[v] = true
+ }
+ }
+ for k, v := range m {
+ if v {
+ return k
+ }
+ }
+ return -1
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output int
+ }{
+ {ints{3, 3, 1}, 1},
+ {ints{3, 2, 4, 2, 4}, 3},
+ {ints{1}, 1},
+ {ints{4, 3, 1, 1, 1, 4}, 3},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.uniqNum(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-283/pokgopun/go/ch-2.go b/challenge-283/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..0174870338
--- /dev/null
+++ b/challenge-283/pokgopun/go/ch-2.go
@@ -0,0 +1,78 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-283/
+/*#
+
+Task 2: Digit Count Value
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of positive integers, @ints.
+
+ Write a script to return true if for every index i in the range 0 <= i
+ < size of array, the digit i occurs exactly the $ints[$i] times in the
+ given array otherwise return false.
+
+Example 1
+
+Input: @ints = (1, 2, 1, 0)
+Ouput: true
+
+$ints[0] = 1, the digit 0 occurs exactly 1 time.
+$ints[1] = 2, the digit 1 occurs exactly 2 times.
+$ints[2] = 1, the digit 2 occurs exactly 1 time.
+$ints[3] = 0, the digit 3 occurs 0 time.
+
+Example 2
+
+Input: @ints = (0, 3, 0)
+Ouput: false
+
+$ints[0] = 0, the digit 0 occurs 2 times rather than 0 time.
+$ints[1] = 3, the digit 1 occurs 0 time rather than 3 times.
+$ints[2] = 0, the digit 2 occurs exactly 0 time.
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 25th August
+ 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) dcv() bool {
+ m := make(map[int]int)
+ for i, v := range is {
+ m[i] += v
+ m[v] -= 1
+ }
+ for _, v := range m {
+ if v != 0 {
+ return false
+ }
+ }
+ return true
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output bool
+ }{
+ {ints{1, 2, 1, 0}, true},
+ {ints{0, 3, 0}, false},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.dcv(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-283/pokgopun/python/ch-1.py b/challenge-283/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..4f40b561ec
--- /dev/null
+++ b/challenge-283/pokgopun/python/ch-1.py
@@ -0,0 +1,55 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-283/
+"""
+
+Task 1: Unique Number
+
+Submitted by: [45]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers, @ints, where every elements appears
+ more than once except one element.
+
+ Write a script to find the one element that appears exactly one time.
+
+Example 1
+
+Input: @ints = (3, 3, 1)
+Output: 1
+
+Example 2
+
+Input: @ints = (3, 2, 4, 2, 4)
+Output: 3
+
+Example 3
+
+Input: @ints = (1)
+Output: 1
+
+Example 4
+
+Input: @ints = (4, 3, 1, 1, 1, 4)
+Output: 3
+
+Task 2: Digit Count Value
+"""
+### solution by pokgopun@gmail.com
+
+def uniqNum(ints):
+ return sorted(
+ (ints.count(e),e) for e in ints
+ )[0][1]
+
+import unittest
+
+class TestUniqNum(unittest.TestCase):
+ def test(self):
+ for inpt,otpt in {
+ (3, 3, 1): 1,
+ (3, 2, 4, 2, 4): 3,
+ (1,): 1,
+ (4, 3, 1, 1, 1, 4): 3,
+ }.items():
+ self.assertEqual(uniqNum(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-283/pokgopun/python/ch-2.py b/challenge-283/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..a867d2d1f9
--- /dev/null
+++ b/challenge-283/pokgopun/python/ch-2.py
@@ -0,0 +1,57 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-283/
+"""
+
+Task 2: Digit Count Value
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of positive integers, @ints.
+
+ Write a script to return true if for every index i in the range 0 <= i
+ < size of array, the digit i occurs exactly the $ints[$i] times in the
+ given array otherwise return false.
+
+Example 1
+
+Input: @ints = (1, 2, 1, 0)
+Ouput: true
+
+$ints[0] = 1, the digit 0 occurs exactly 1 time.
+$ints[1] = 2, the digit 1 occurs exactly 2 times.
+$ints[2] = 1, the digit 2 occurs exactly 1 time.
+$ints[3] = 0, the digit 3 occurs 0 time.
+
+Example 2
+
+Input: @ints = (0, 3, 0)
+Ouput: false
+
+$ints[0] = 0, the digit 0 occurs 2 times rather than 0 time.
+$ints[1] = 3, the digit 1 occurs 0 time rather than 3 times.
+$ints[2] = 0, the digit 2 occurs exactly 0 time.
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 25th August
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def dcv(ints):
+ idx = range(len(ints))
+ return set((i,ints.count(i)) for i in idx) == set((i,ints[i]) for i in idx)
+
+import unittest
+
+class TetDcv(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ (1, 2, 1, 0): True,
+ (0, 3, 0): False,
+ }.items():
+ self.assertEqual(dcv(inpt),otpt)
+
+unittest.main()