aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-01 14:04:44 +0000
committerGitHub <noreply@github.com>2024-01-01 14:04:44 +0000
commit0d024c3d22847623af662c4219d5b06bd5c9b353 (patch)
treea87d26987986684d4ac5c3999f392050fc0fa9bd
parentf98d2a12daf1726b7a84c3f61bfadac82fc720c4 (diff)
parent8218ddbc7eeeb5b89fa09b5413ce4dd6e1e546a2 (diff)
downloadperlweeklychallenge-club-0d024c3d22847623af662c4219d5b06bd5c9b353.tar.gz
perlweeklychallenge-club-0d024c3d22847623af662c4219d5b06bd5c9b353.tar.bz2
perlweeklychallenge-club-0d024c3d22847623af662c4219d5b06bd5c9b353.zip
Merge pull request #9324 from pokgopun/pwc250
pwc250 solution
-rw-r--r--challenge-250/pokgopun/go/ch-1.go76
-rw-r--r--challenge-250/pokgopun/go/ch-2.go88
-rw-r--r--challenge-250/pokgopun/python/ch-1.py64
-rw-r--r--challenge-250/pokgopun/python/ch-2.py60
4 files changed, 288 insertions, 0 deletions
diff --git a/challenge-250/pokgopun/go/ch-1.go b/challenge-250/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..68c565b258
--- /dev/null
+++ b/challenge-250/pokgopun/go/ch-1.go
@@ -0,0 +1,76 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-250/
+/*#
+
+Task 1: Smallest Index
+
+Submitted by: [38]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers, @ints.
+
+ Write a script to find the smallest index i such that i mod 10 ==
+ $ints[i] otherwise return -1.
+
+Example 1
+
+Input: @ints = (0, 1, 2)
+Output: 0
+
+i=0: 0 mod 10 = 0 == $ints[0].
+i=1: 1 mod 10 = 1 == $ints[1].
+i=2: 2 mod 10 = 2 == $ints[2].
+All indices have i mod 10 == $ints[i], so we return the smallest index 0.
+
+Example 2
+
+Input: @ints = (4, 3, 2, 1)
+Output: 2
+
+i=0: 0 mod 10 = 0 != $ints[0].
+i=1: 1 mod 10 = 1 != $ints[1].
+i=2: 2 mod 10 = 2 == $ints[2].
+i=3: 3 mod 10 = 3 != $ints[3].
+2 is the only index which has i mod 10 == $ints[i].
+
+Example 3
+
+Input: @ints = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
+Output: -1
+Explanation: No index satisfies i mod 10 == $ints[i].
+
+Task 2: Alphanumeric String Value
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (is ints) smallestIndex() int {
+ for i, v := range is {
+ if i%10 == v {
+ return i
+ }
+ }
+ return -1
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output int
+ }{
+ {ints{0, 1, 2}, 0},
+ {ints{4, 3, 2, 1}, 2},
+ {ints{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, -1},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.smallestIndex(), data.output)) // output nothing if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-250/pokgopun/go/ch-2.go b/challenge-250/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..cb85858dc1
--- /dev/null
+++ b/challenge-250/pokgopun/go/ch-2.go
@@ -0,0 +1,88 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-250/
+/*#
+
+Task 2: Alphanumeric String Value
+
+Submitted by: [39]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of alphanumeric strings.
+
+ Write a script to return the maximum value of alphanumeric string in
+ the given array.
+
+ The value of alphanumeric string can be defined as
+a) The numeric representation of the string in base 10 if it is made up of digit
+s only.
+b) otherwise the length of the string
+
+Example 1
+
+Input: @alphanumstr = ("perl", "2", "000", "python", "r4ku")
+Output: 6
+
+"perl" consists of letters only so the value is 4.
+"2" is digits only so the value is 2.
+"000" is digits only so the value is 0.
+"python" consits of letters so the value is 6.
+"r4ku" consists of letters and digits so the value is 4.
+
+Example 2
+
+Input: @alphanumstr = ("001", "1", "000", "0001")
+Output: 1
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 7th January
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "strconv"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func main() {
+ for _, data := range []struct {
+ input strs
+ output int
+ }{
+ {strs{"perl", "2", "000", "python", "r4ku"}, 6},
+ {strs{"001", "1", "000", "0001"}, 1},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.max(), data.output)) // output nothing if ok, otherwise show the difference
+ }
+}
+
+type str string
+
+func (s str) value() int {
+ r, err := strconv.Atoi(string(s))
+ if err != nil {
+ return len(s)
+ }
+ return r
+}
+
+type strs []str
+
+func (ss strs) max() int {
+ var mx, c int
+ for _, v := range ss {
+ c = v.value()
+ //fmt.Println(v, "=>", c)
+ if mx < c {
+ mx = c
+ }
+ }
+ return mx
+}
diff --git a/challenge-250/pokgopun/python/ch-1.py b/challenge-250/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..b1450390ef
--- /dev/null
+++ b/challenge-250/pokgopun/python/ch-1.py
@@ -0,0 +1,64 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-250/
+"""
+
+Task 1: Smallest Index
+
+Submitted by: [38]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers, @ints.
+
+ Write a script to find the smallest index i such that i mod 10 ==
+ $ints[i] otherwise return -1.
+
+Example 1
+
+Input: @ints = (0, 1, 2)
+Output: 0
+
+i=0: 0 mod 10 = 0 == $ints[0].
+i=1: 1 mod 10 = 1 == $ints[1].
+i=2: 2 mod 10 = 2 == $ints[2].
+All indices have i mod 10 == $ints[i], so we return the smallest index 0.
+
+Example 2
+
+Input: @ints = (4, 3, 2, 1)
+Output: 2
+
+i=0: 0 mod 10 = 0 != $ints[0].
+i=1: 1 mod 10 = 1 != $ints[1].
+i=2: 2 mod 10 = 2 == $ints[2].
+i=3: 3 mod 10 = 3 != $ints[3].
+2 is the only index which has i mod 10 == $ints[i].
+
+Example 3
+
+Input: @ints = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
+Output: -1
+Explanation: No index satisfies i mod 10 == $ints[i].
+
+Task 2: Alphanumeric String Value
+"""
+### solution by pokgopun@gmail.com
+
+def smallestIndex(tup: tuple):
+ for i in range(len(tup)):
+ if i % 10 == tup[i]:
+ return i
+ return -1
+
+import unittest
+
+class TestSmallestIndex(unittest.TestCase):
+ def test1(self):
+ for inpt,otpt in {
+ (0, 1, 2): 0,
+ (4, 3, 2, 1): 2,
+ (1, 2, 3, 4, 5, 6, 7, 8, 9, 0): -1,
+ }.items():
+ self.assertEqual(smallestIndex(inpt),otpt)
+
+unittest.main()
+
+
diff --git a/challenge-250/pokgopun/python/ch-2.py b/challenge-250/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..eaaba8cab1
--- /dev/null
+++ b/challenge-250/pokgopun/python/ch-2.py
@@ -0,0 +1,60 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-250/
+"""
+
+Task 2: Alphanumeric String Value
+
+Submitted by: [39]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of alphanumeric strings.
+
+ Write a script to return the maximum value of alphanumeric string in
+ the given array.
+
+ The value of alphanumeric string can be defined as
+a) The numeric representation of the string in base 10 if it is made up of digit
+s only.
+b) otherwise the length of the string
+
+Example 1
+
+Input: @alphanumstr = ("perl", "2", "000", "python", "r4ku")
+Output: 6
+
+"perl" consists of letters only so the value is 4.
+"2" is digits only so the value is 2.
+"000" is digits only so the value is 0.
+"python" consits of letters so the value is 6.
+"r4ku" consists of letters and digits so the value is 4.
+
+Example 2
+
+Input: @alphanumstr = ("001", "1", "000", "0001")
+Output: 1
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 7th January
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def alphanumericStringValue(tup: tuple):
+ return max(
+ int(e) if e.isnumeric() else len(e) for e in tup
+ )
+
+import unittest
+
+class TestAlphanumericStringValue(unittest.TestCase):
+ def test(self):
+ for inpt,otpt in {
+ ("perl", "2", "000", "python", "r4ku"): 6,
+ ("001", "1", "000", "0001"): 1,
+ }.items():
+ self.assertEqual(alphanumericStringValue(inpt),otpt)
+
+unittest.main()
+