aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-07-16 09:34:14 +0100
committerGitHub <noreply@github.com>2025-07-16 09:34:14 +0100
commitca383a3a6deb0bf4f9008359de189661b0e2affa (patch)
treeec275efacdfce7e8d8a011d282d5b265ee5ab841
parent0b4045d268cb224b6b57dee2465104fcec6e9c33 (diff)
parentaae700b856b280e75ae989cea61539fe0e03894e (diff)
downloadperlweeklychallenge-club-ca383a3a6deb0bf4f9008359de189661b0e2affa.tar.gz
perlweeklychallenge-club-ca383a3a6deb0bf4f9008359de189661b0e2affa.tar.bz2
perlweeklychallenge-club-ca383a3a6deb0bf4f9008359de189661b0e2affa.zip
Merge pull request #12359 from pokgopun/pwc330
Pwc330
-rw-r--r--challenge-330/pokgopun/go/ch-1.go72
-rw-r--r--challenge-330/pokgopun/go/ch-2.go72
-rw-r--r--challenge-330/pokgopun/python/ch-1.py61
-rw-r--r--challenge-330/pokgopun/python/ch-2.py54
4 files changed, 259 insertions, 0 deletions
diff --git a/challenge-330/pokgopun/go/ch-1.go b/challenge-330/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..d3ac266346
--- /dev/null
+++ b/challenge-330/pokgopun/go/ch-1.go
@@ -0,0 +1,72 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-330/
+/*#
+
+Task 1: Clear Digits
+
+Submitted by: [44]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string containing only lower case English letters and
+ digits.
+
+ Write a script to remove all digits by removing the first digit and the
+ closest non-digit character to its left.
+
+Example 1
+
+Input: $str = "cab12"
+Output: "c"
+
+Round 1: remove "1" then "b" => "ca2"
+Round 2: remove "2" then "a" => "c"
+
+Example 2
+
+Input: $str = "xy99"
+Output: ""
+
+Round 1: remove "9" then "y" => "x9"
+Round 2: remove "9" then "x" => ""
+
+Example 3
+
+Input: $str = "pa1erl"
+Output: "perl"
+
+Task 2: Title Capital
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "regexp"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func cd(str string) string {
+ re := regexp.MustCompile(`([a-z])(\d*?)(\d)`)
+ for {
+ s := re.ReplaceAllString(str, "${2}")
+ if s == str {
+ break
+ }
+ str = s
+ }
+ return str
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output string
+ }{
+ {"cab12", "c"},
+ {"xy99", ""},
+ {"pa1erl", "perl"},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(cd(data.input), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-330/pokgopun/go/ch-2.go b/challenge-330/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..31df8c7025
--- /dev/null
+++ b/challenge-330/pokgopun/go/ch-2.go
@@ -0,0 +1,72 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-330/
+/*#
+
+Task 2: Title Capital
+
+Submitted by: [45]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string made up of one or more words separated by a
+ single space.
+
+ Write a script to capitalise the given title. If the word length is 1
+ or 2 then convert the word to lowercase otherwise make the first
+ character uppercase and remaining lowercase.
+
+Example 1
+
+Input: $str = "PERL IS gREAT"
+Output: "Perl is Great"
+
+Example 2
+
+Input: $str = "THE weekly challenge"
+Output: "The Weekly Challenge"
+
+Example 3
+
+Input: $str = "YoU ARE A stAR"
+Output: "You Are a Star"
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 20th July 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "strings"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func tc(str string) string {
+ var b strings.Builder
+ for v := range strings.SplitSeq(strings.ToLower(str), " ") {
+ b.WriteRune(' ')
+ if len(v) > 2 {
+ b.WriteString(strings.ToUpper(v[:1]) + v[1:])
+ } else {
+ b.WriteString(v)
+ }
+ }
+ return b.String()[1:]
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output string
+ }{
+ {"PERL IS gREAT", "Perl is Great"},
+ {"THE weekly challenge", "The Weekly Challenge"},
+ {"YoU ARE A stAR", "You Are a Star"},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(tc(data.input), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-330/pokgopun/python/ch-1.py b/challenge-330/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..4bf81257c7
--- /dev/null
+++ b/challenge-330/pokgopun/python/ch-1.py
@@ -0,0 +1,61 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-330/
+"""
+
+Task 1: Clear Digits
+
+Submitted by: [44]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string containing only lower case English letters and
+ digits.
+
+ Write a script to remove all digits by removing the first digit and the
+ closest non-digit character to its left.
+
+Example 1
+
+Input: $str = "cab12"
+Output: "c"
+
+Round 1: remove "1" then "b" => "ca2"
+Round 2: remove "2" then "a" => "c"
+
+Example 2
+
+Input: $str = "xy99"
+Output: ""
+
+Round 1: remove "9" then "y" => "x9"
+Round 2: remove "9" then "x" => ""
+
+Example 3
+
+Input: $str = "pa1erl"
+Output: "perl"
+
+Task 2: Title Capital
+"""
+### solution by pokgopun@gmail.com
+
+import re
+
+def cd(string: str) -> str:
+ while True:
+ s = re.sub(r'([a-z])([^a-z]*?)(\d)',r'\2',string)
+ if s == string:
+ break
+ string = s
+ return s
+
+import unittest
+
+class TestCd(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ "cab12": "c",
+ "xy99": "",
+ "pa1erl": "perl",
+ }.items():
+ self.assertEqual(cd(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-330/pokgopun/python/ch-2.py b/challenge-330/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..8ad2dd66af
--- /dev/null
+++ b/challenge-330/pokgopun/python/ch-2.py
@@ -0,0 +1,54 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-330/
+"""
+
+Task 2: Title Capital
+
+Submitted by: [45]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string made up of one or more words separated by a
+ single space.
+
+ Write a script to capitalise the given title. If the word length is 1
+ or 2 then convert the word to lowercase otherwise make the first
+ character uppercase and remaining lowercase.
+
+Example 1
+
+Input: $str = "PERL IS gREAT"
+Output: "Perl is Great"
+
+Example 2
+
+Input: $str = "THE weekly challenge"
+Output: "The Weekly Challenge"
+
+Example 3
+
+Input: $str = "YoU ARE A stAR"
+Output: "You Are a Star"
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 20th July 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def tc(string: str) -> str:
+ return " ".join( e.title() if len(e) > 2 else e for e in string.lower().split() )
+
+import unittest
+
+class TestTc(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ "PERL IS gREAT": "Perl is Great",
+ "THE weekly challenge": "The Weekly Challenge",
+ "YoU ARE A stAR": "You Are a Star",
+ }.items():
+ self.assertEqual(tc(inpt),otpt)
+
+unittest.main()
+