aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-07-08 17:01:31 +0100
committerGitHub <noreply@github.com>2025-07-08 17:01:31 +0100
commit8c30b83673e2f1b84271497e35f222c6fd4c3958 (patch)
tree3e3dc76202edf5d36d00c575f4f83efc41939725
parent1e272a7972b48785d00a851aad8a13b200b043f2 (diff)
parentfbe98362987fc353e0d39127b4d502ab3b040656 (diff)
downloadperlweeklychallenge-club-8c30b83673e2f1b84271497e35f222c6fd4c3958.tar.gz
perlweeklychallenge-club-8c30b83673e2f1b84271497e35f222c6fd4c3958.tar.bz2
perlweeklychallenge-club-8c30b83673e2f1b84271497e35f222c6fd4c3958.zip
Merge pull request #12303 from pokgopun/pwc329
Pwc329
-rw-r--r--challenge-329/pokgopun/go/ch-1.go74
-rw-r--r--challenge-329/pokgopun/go/ch-2.go96
-rw-r--r--challenge-329/pokgopun/python/ch-1.py54
-rw-r--r--challenge-329/pokgopun/python/ch-2.py73
4 files changed, 297 insertions, 0 deletions
diff --git a/challenge-329/pokgopun/go/ch-1.go b/challenge-329/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..1469d78265
--- /dev/null
+++ b/challenge-329/pokgopun/go/ch-1.go
@@ -0,0 +1,74 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-329/
+/*#
+
+Task 1: Counter Integers
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string containing only lower case English letters and
+ digits.
+
+ Write a script to replace every non-digit character with a space and
+ then return all the distinct integers left.
+
+Example 1
+
+Input: $str = "the1weekly2challenge2"
+Output: 1, 2
+
+2 is appeared twice, so we count it one only.
+
+Example 2
+
+Input: $str = "go21od1lu5c7k"
+Output: 21, 1, 5, 7
+
+Example 3
+
+Input: $str = "4p3e2r1l"
+Output: 4, 3, 2, 1
+
+Task 2: Nice String
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "regexp"
+ "strconv"
+ "strings"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type input string
+
+func (in input) process() []int {
+ var out []int
+ seen := make(map[string]struct{})
+ for _, v := range strings.Split(strings.Trim(regexp.MustCompile(`\D+`).ReplaceAllString(string(in), " "), " "), " ") {
+ if _, ok := seen[v]; !ok {
+ seen[v] = struct{}{}
+ n, _ := strconv.Atoi(v)
+ out = append(out, n)
+ }
+ }
+ return out
+}
+
+func main() {
+ for _, data := range []struct {
+ input input
+ output []int
+ }{
+ {"the1weekly2challenge2", []int{1, 2}},
+ {"go21od1lu5c7k", []int{21, 1, 5, 7}},
+ {"4p3e2r1l", []int{4, 3, 2, 1}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-329/pokgopun/go/ch-2.go b/challenge-329/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..f7df4ef034
--- /dev/null
+++ b/challenge-329/pokgopun/go/ch-2.go
@@ -0,0 +1,96 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-329/
+/*#
+
+Task 2: Nice String
+
+Submitted by: [47]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string made up of lower and upper case English letters
+ only.
+
+ Write a script to return the longest substring of the give string which
+ is nice. A string is nice if, for every letter of the alphabet that the
+ string contains, it appears both in uppercase and lowercase.
+
+Example 1
+
+Input: $str = "YaaAho"
+Output: "aaA"
+
+Example 2
+
+Input: $str = "cC"
+Output: "cC"
+
+Example 3
+
+Input: $str = "A"
+Output: ""
+
+No nice string found.
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 13th July 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "slices"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type str string
+
+func (s str) process() str {
+ var out str
+ l := len(s) - 1
+ i := l
+ start := l - i
+ for i > 0 {
+ i--
+ idx := l - i
+ a, b := s[idx], s[idx-1]
+ d := max(a, b) - min(a, b)
+ if d == 0 || d == 32 {
+ continue
+ } else {
+ cdd := s[start:idx]
+ if len(cdd) > len(out) && len(slices.Compact([]byte(cdd))) > 1 {
+ out = cdd
+ }
+ start = idx
+ }
+ }
+ if i == 0 {
+ cdd := s[start:]
+ if len(cdd) > len(out) && len(slices.Compact([]byte(cdd))) > 1 {
+ out = cdd
+ }
+ }
+ return out
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output str
+ }{
+ {"YaaAho", "aaA"},
+ {"YaaAhoooo", "aaA"},
+ {"YaaAhoOOo", "oOOo"},
+ {"YaaAhooO", "aaA"},
+ {"cC", "cC"},
+ {"cc", ""},
+ {"A", ""},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-329/pokgopun/python/ch-1.py b/challenge-329/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..c3bc755523
--- /dev/null
+++ b/challenge-329/pokgopun/python/ch-1.py
@@ -0,0 +1,54 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-329/
+"""
+
+Task 1: Counter Integers
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string containing only lower case English letters and
+ digits.
+
+ Write a script to replace every non-digit character with a space and
+ then return all the distinct integers left.
+
+Example 1
+
+Input: $str = "the1weekly2challenge2"
+Output: 1, 2
+
+2 is appeared twice, so we count it one only.
+
+Example 2
+
+Input: $str = "go21od1lu5c7k"
+Output: 21, 1, 5, 7
+
+Example 3
+
+Input: $str = "4p3e2r1l"
+Output: 4, 3, 2, 1
+
+Task 2: Nice String
+"""
+### solution by pokgopun@gmail.com
+
+import re
+
+def ci(string: str) -> tuple[int]:
+ seen = dict()
+ return tuple(seen.setdefault(d,int(d)) for d in re.sub(r"\D+", " ", string).strip().split()
+ if seen.get(d) == None)
+
+import unittest
+
+class TestCi(unittest.TestCase):
+ def test(self):
+ for inpt,otpt in {
+ "the1weekly2challenge2": (1, 2),
+ "go21od1lu5c7k": (21, 1, 5, 7),
+ "4p3e2r1l": (4, 3, 2, 1),
+ }.items():
+ self.assertEqual(ci(inpt), otpt)
+
+unittest.main()
diff --git a/challenge-329/pokgopun/python/ch-2.py b/challenge-329/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..d1913edfba
--- /dev/null
+++ b/challenge-329/pokgopun/python/ch-2.py
@@ -0,0 +1,73 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-329/
+"""
+
+Task 2: Nice String
+
+Submitted by: [47]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string made up of lower and upper case English letters
+ only.
+
+ Write a script to return the longest substring of the give string which
+ is nice. A string is nice if, for every letter of the alphabet that the
+ string contains, it appears both in uppercase and lowercase.
+
+Example 1
+
+Input: $str = "YaaAho"
+Output: "aaA"
+
+Example 2
+
+Input: $str = "cC"
+Output: "cC"
+
+Example 3
+
+Input: $str = "A"
+Output: ""
+
+No nice string found.
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 13th July 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def ns(string: str) -> str:
+ start = 0
+ nice = ""
+ for i in range(1,len(string)):
+ if string[i].lower() == string[i-1].lower():
+ continue
+ else:
+ cdd = string[start:i]
+ if len(set(cdd)) > 1 and len(cdd) > len(nice):
+ nice = cdd
+ start = i
+ else:
+ cdd = string[start:]
+ if len(set(cdd)) > 1 and len(cdd) > len(nice):
+ nice = cdd
+ return nice
+
+import unittest
+
+class TestNs(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ "YaaAho": "aaA",
+ "YaaAhoooo": "aaA",
+ "YaaAhoOOo": "oOOo",
+ "YaaAhooO": "aaA",
+ "cC": "cC",
+ "cc": "",
+ "A": "",
+ }.items():
+ self.assertEqual(ns(inpt),otpt)
+
+unittest.main()