aboutsummaryrefslogtreecommitdiff
path: root/challenge-256
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-13 01:02:36 +0000
committerGitHub <noreply@github.com>2024-02-13 01:02:36 +0000
commit373343e0153c35b2c1b0177a9c7370b9b04bc642 (patch)
tree23eb84bf1e637d210a6513174eb9faa0f9196e92 /challenge-256
parent38e27dd8b3d10f4669dd88c2b2a32bae28ee91ed (diff)
parent2f29832746cd89b07b6fee887676fa2bc7a578d6 (diff)
downloadperlweeklychallenge-club-373343e0153c35b2c1b0177a9c7370b9b04bc642.tar.gz
perlweeklychallenge-club-373343e0153c35b2c1b0177a9c7370b9b04bc642.tar.bz2
perlweeklychallenge-club-373343e0153c35b2c1b0177a9c7370b9b04bc642.zip
Merge pull request #9567 from pokgopun/pwc256
pwc256 solution
Diffstat (limited to 'challenge-256')
-rw-r--r--challenge-256/pokgopun/go/ch-1.go80
-rw-r--r--challenge-256/pokgopun/go/ch-2.go80
-rw-r--r--challenge-256/pokgopun/python/ch-1.py54
-rw-r--r--challenge-256/pokgopun/python/ch-2.py59
4 files changed, 273 insertions, 0 deletions
diff --git a/challenge-256/pokgopun/go/ch-1.go b/challenge-256/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..b52df3379b
--- /dev/null
+++ b/challenge-256/pokgopun/go/ch-1.go
@@ -0,0 +1,80 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-256/
+/*#
+
+Task 1: Maximum Pairs
+
+Submitted by: [41]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of distinct words, @words.
+
+ Write a script to find the maximum pairs in the given array. The words
+ $words[i] and $words[j] can be a pair one is reverse of the other.
+
+Example 1
+
+Input: @words = ("ab", "de", "ed", "bc")
+Output: 1
+
+There is one pair in the given array: "de" and "ed"
+
+Example 2
+
+Input: @words = ("aa", "ba", "cd", "ed")
+Output: 0
+
+Example 3
+
+Input: @words = ("uv", "qp", "st", "vu", "mn", "pq"))
+Output: 2
+
+Task 2: Merge Strings
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "slices"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type str string
+
+func (s str) reverse() str {
+ r := []rune(s)
+ slices.Reverse(r)
+ return str(r)
+}
+
+type strs []str
+
+func (st strs) maxPair() int {
+ l := len(st)
+ var c, i int
+ for i < l-1 {
+ for _, v := range st[i+1:] {
+ if st[i] == v.reverse() {
+ c++
+ }
+ }
+ i++
+ }
+ return c
+}
+
+func main() {
+ for _, data := range []struct {
+ input strs
+ output int
+ }{
+ {strs{"ab", "de", "ed", "bc"}, 1},
+ {strs{"aa", "ba", "cd", "ed"}, 0},
+ {strs{"uv", "qp", "st", "vu", "mn", "pq"}, 2},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.maxPair(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-256/pokgopun/go/ch-2.go b/challenge-256/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..eb931d9d7e
--- /dev/null
+++ b/challenge-256/pokgopun/go/ch-2.go
@@ -0,0 +1,80 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-256/
+/*#
+
+Task 2: Merge Strings
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given two strings, $str1 and $str2.
+
+ Write a script to merge the given strings by adding in alternative
+ order starting with the first string. If a string is longer than the
+ other then append the remaining at the end.
+
+Example 1
+
+Input: $str1 = "abcd", $str2 = "1234"
+Output: "a1b2c3d4"
+
+Example 2
+
+Input: $str1 = "abc", $str2 = "12345"
+Output: "a1b2c345"
+
+Example 3
+
+Input: $str1 = "abcde", $str2 = "123"
+Output: "a1b2c3de"
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 18th February
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "strings"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type strPair struct {
+ str1, str2 string
+}
+
+func (sp strPair) merge() string {
+ l1 := len(sp.str1)
+ l2 := len(sp.str2)
+ l := max(l1, l2)
+ var b strings.Builder
+ for i := 0; i < l; i++ {
+ if i < l1 {
+ b.WriteByte(sp.str1[i])
+ }
+ if i < l2 {
+ b.WriteByte(sp.str2[i])
+ }
+ }
+ return b.String()
+}
+
+func main() {
+ for _, data := range []struct {
+ input strPair
+ output string
+ }{
+ {strPair{"abcd", "1234"}, "a1b2c3d4"},
+ {strPair{"abc", "12345"}, "a1b2c345"},
+ {strPair{"abcde", "123"}, "a1b2c3de"},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.merge(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-256/pokgopun/python/ch-1.py b/challenge-256/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..09587e16af
--- /dev/null
+++ b/challenge-256/pokgopun/python/ch-1.py
@@ -0,0 +1,54 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-256/
+"""
+
+Task 1: Maximum Pairs
+
+Submitted by: [41]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of distinct words, @words.
+
+ Write a script to find the maximum pairs in the given array. The words
+ $words[i] and $words[j] can be a pair one is reverse of the other.
+
+Example 1
+
+Input: @words = ("ab", "de", "ed", "bc")
+Output: 1
+
+There is one pair in the given array: "de" and "ed"
+
+Example 2
+
+Input: @words = ("aa", "ba", "cd", "ed")
+Output: 0
+
+Example 3
+
+Input: @words = ("uv", "qp", "st", "vu", "mn", "pq"))
+Output: 2
+
+Task 2: Merge Strings
+"""
+### solution by pokgopun@gmail.com
+
+def maxPair(words: tuple):
+ c = 0
+ for i in range(len(words)-1):
+ for e in words[i+1:]:
+ if tuple(words[i])==tuple(reversed(e)):
+ c += 1
+ return c
+
+import unittest
+
+class TestMaxPair(unittest.TestCase):
+ def test(self):
+ for inpt,otpt in {
+ ("ab", "de", "ed", "bc"): 1,
+ ("aa", "ba", "cd", "ed"): 0,
+ ("uv", "qp", "st", "vu", "mn", "pq"): 2,
+ }.items():
+ self.assertEqual(maxPair(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-256/pokgopun/python/ch-2.py b/challenge-256/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..e32ae2557a
--- /dev/null
+++ b/challenge-256/pokgopun/python/ch-2.py
@@ -0,0 +1,59 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-256/
+"""
+
+Task 2: Merge Strings
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given two strings, $str1 and $str2.
+
+ Write a script to merge the given strings by adding in alternative
+ order starting with the first string. If a string is longer than the
+ other then append the remaining at the end.
+
+Example 1
+
+Input: $str1 = "abcd", $str2 = "1234"
+Output: "a1b2c3d4"
+
+Example 2
+
+Input: $str1 = "abc", $str2 = "12345"
+Output: "a1b2c345"
+
+Example 3
+
+Input: $str1 = "abcde", $str2 = "123"
+Output: "a1b2c3de"
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 18th February
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+from itertools import zip_longest,chain
+
+def mergeStrings(str1, str2):
+ return "".join(
+ chain.from_iterable(
+ zip_longest(str1,str2,fillvalue="")
+ )
+ )
+
+import unittest
+
+class TestMergeStrings(unittest.TestCase):
+ def test(self):
+ for (str1,str2), otpt in {
+ ("abcd","1234"): "a1b2c3d4",
+ ("abc", "12345"): "a1b2c345",
+ ("abcde", "123"): "a1b2c3de",
+ }.items():
+ self.assertEqual(mergeStrings(str1,str2),otpt)
+
+unittest.main()