aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-07 19:55:58 +0100
committerGitHub <noreply@github.com>2025-10-07 19:55:58 +0100
commit39664bf797d23a39d953acb5b6f8419437196402 (patch)
treeba7e4a5ed98f2a13dec483ff9b17d6e3470783db
parent7b4581f2a994030befcc6d50e0b6735e0e9d660f (diff)
parentcb51428ba58f6c68f6e930f142185fb496040dd1 (diff)
downloadperlweeklychallenge-club-39664bf797d23a39d953acb5b6f8419437196402.tar.gz
perlweeklychallenge-club-39664bf797d23a39d953acb5b6f8419437196402.tar.bz2
perlweeklychallenge-club-39664bf797d23a39d953acb5b6f8419437196402.zip
Merge pull request #12810 from pokgopun/pwc342
Pwc342
-rw-r--r--challenge-342/pokgopun/go/ch-1.go97
-rw-r--r--challenge-342/pokgopun/go/ch-2.go133
-rw-r--r--challenge-342/pokgopun/python/ch-1.py74
-rw-r--r--challenge-342/pokgopun/python/ch-2.py109
4 files changed, 413 insertions, 0 deletions
diff --git a/challenge-342/pokgopun/go/ch-1.go b/challenge-342/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..6cb71202a1
--- /dev/null
+++ b/challenge-342/pokgopun/go/ch-1.go
@@ -0,0 +1,97 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-342/
+/*#
+
+Task 1: Balance String
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string made up of lowercase English letters and digits
+ only.
+
+ Write a script to format the give string where no letter is followed by
+ another letter and no digit is followed by another digit. If there are
+ multiple valid rearrangements, always return the lexicographically
+ smallest one. Return empty string if it is impossible to format the
+ string.
+
+Example 1
+
+Input: $str = "a0b1c2"
+Output: "0a1b2c"
+
+Example 2
+
+Input: $str = "abc12"
+Output: "a1b2c"
+
+Example 3
+
+Input: $str = "0a2b1c3"
+Output: "0a1b2c3"
+
+Example 4
+
+Input: $str = "1a23"
+Output: ""
+
+Example 5
+
+Input: $str = "ab123"
+Output: "1a2b3"
+
+Task 2: Max Score
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "slices"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func bs(str string) string {
+ rs := append([]rune(str), '_')
+ slices.Sort(rs)
+ m := slices.Index(rs, '_')
+ l := len(str)
+ d := l - 2*m
+ switch d {
+ case -1, 0:
+ copy(rs[m:], rs[m+1:])
+ case 1:
+ for i := range m + 1 {
+ rs[i], rs[m+i+1] = rs[m+i+1], rs[i]
+ }
+ default:
+ return ""
+ }
+ n := max(m+d, m)
+ for i := 1; i < n; i++ {
+ for j := i; j < n; j++ {
+ rs[j], rs[n+j-i] = rs[n+j-i], rs[j]
+ }
+ }
+ return string(rs[:l])
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output string
+ }{
+ {"a0b1c2", "0a1b2c"},
+ {"abc12", "a1b2c"},
+ {"0a2b1c3", "0a1b2c3"},
+ {"1a23", ""},
+ {"ab123", "1a2b3"},
+ {"abcdefg0123456", "0a1b2c3d4e5f6g"},
+ {"abcdef0123456", "0a1b2c3d4e5f6"},
+ {"abcdefg123456", "a1b2c3d4e5f6g"},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(bs(data.input), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-342/pokgopun/go/ch-2.go b/challenge-342/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..e0b69fc13c
--- /dev/null
+++ b/challenge-342/pokgopun/go/ch-2.go
@@ -0,0 +1,133 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-342/
+/*#
+
+Task 2: Max Score
+
+Submitted by: [47]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str, containing 0 and 1 only.
+
+ Write a script to return the max score after splitting the string into
+ two non-empty substrings. The score after splitting a string is the
+ number of zeros in the left substring plus the number of ones in the
+ right substring.
+
+Example 1
+
+Input: $str = "0011"
+Output: 4
+
+1: left = "0", right = "011" => 1 + 2 => 3
+2: left = "00", right = "11" => 2 + 2 => 4
+3: left = "001", right = "1" => 2 + 1 => 3
+
+Example 2
+
+Input: $str = "0000"
+Output: 3
+
+1: left = "0", right = "000" => 1 + 0 => 1
+2: left = "00", right = "00" => 2 + 0 => 2
+3: left = "000", right = "0" => 3 + 0 => 3
+
+Example 3
+
+Input: $str = "1111"
+Output: 3
+
+1: left = "1", right = "111" => 0 + 3 => 3
+2: left = "11", right = "11" => 0 + 2 => 2
+3: left = "111", right = "1" => 0 + 1 => 1
+
+Example 4
+
+Input: $str = "0101"
+Output: 3
+
+1: left = "0", right = "101" => 1 + 2 => 3
+2: left = "01", right = "01" => 1 + 1 => 2
+3: left = "010", right = "1" => 2 + 1 => 3
+
+Example 5
+
+Input: $str = "011101"
+Output: 5
+
+1: left = "0", right = "11101" => 1 + 4 => 5
+2: left = "01", right = "1101" => 1 + 3 => 4
+3: left = "011", right = "101" => 1 + 2 => 3
+4: left = "0111", right = "01" => 1 + 1 => 2
+5: left = "01110", right = "1" => 2 + 1 => 3
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 12th October
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "strings"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func ms(str string) int {
+ mx := 0
+ for i := range len(str) - 1 {
+ sm := strings.Count(str[:i+1], "0") + strings.Count(str[i+1:], "1")
+ if mx < sm {
+ mx = sm
+ }
+ }
+ return mx
+ /*
+ l := len(str)
+ rs := []rune(str)
+ off := make([]bool, l)
+ sm := 0
+ for i := range l {
+ if rs[i] == '0' {
+ sm++
+ } else {
+ off[i] = true
+ }
+ }
+ mx := 0
+ for l > 1 {
+ l--
+ if off[l] {
+ sm++
+ } else {
+ sm--
+ }
+ if mx < sm {
+ mx = sm
+ }
+ }
+ return mx
+ */
+}
+
+func main() {
+ for _, data := range []struct {
+ input string
+ output int
+ }{
+ {"0011", 4},
+ {"0000", 3},
+ {"1111", 3},
+ {"0101", 3},
+ {"011101", 5},
+ {"011111011111011111011111011111011111011111011111011111011111011111011111011111011111011111011111011111", 86},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(ms(data.input), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-342/pokgopun/python/ch-1.py b/challenge-342/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..ae60678622
--- /dev/null
+++ b/challenge-342/pokgopun/python/ch-1.py
@@ -0,0 +1,74 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-342/
+"""
+
+Task 1: Balance String
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string made up of lowercase English letters and digits
+ only.
+
+ Write a script to format the give string where no letter is followed by
+ another letter and no digit is followed by another digit. If there are
+ multiple valid rearrangements, always return the lexicographically
+ smallest one. Return empty string if it is impossible to format the
+ string.
+
+Example 1
+
+Input: $str = "a0b1c2"
+Output: "0a1b2c"
+
+Example 2
+
+Input: $str = "abc12"
+Output: "a1b2c"
+
+Example 3
+
+Input: $str = "0a2b1c3"
+Output: "0a1b2c3"
+
+Example 4
+
+Input: $str = "1a23"
+Output: ""
+
+Example 5
+
+Input: $str = "ab123"
+Output: "1a2b3"
+
+Task 2: Max Score
+"""
+### solution by pokgopun@gmail.com
+
+def bs(string: str) -> str:
+ chars = sorted(string + "_")
+ m = chars.index("_")
+ l = len(string)
+ d = l - 2*m
+ match d:
+ case 0:
+ return "".join( chars[i] + chars[m+i+1] for i in range(m) )
+ case 1:
+ return "".join( chars[m+i+1] + chars[i] for i in range(m) ) + chars[l]
+ case -1:
+ return "".join( chars[i] + chars[m+i+1] for i in range(m-1) ) + chars[m-1]
+ return ""
+
+import unittest
+
+class TestBs(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ "a0b1c2": "0a1b2c",
+ "abc12": "a1b2c",
+ "0a2b1c3": "0a1b2c3",
+ "1a23": "",
+ "ab123": "1a2b3",
+ }.items():
+ self.assertEqual(bs(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-342/pokgopun/python/ch-2.py b/challenge-342/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..250ee804aa
--- /dev/null
+++ b/challenge-342/pokgopun/python/ch-2.py
@@ -0,0 +1,109 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-342/
+"""
+
+Task 2: Max Score
+
+Submitted by: [47]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str, containing 0 and 1 only.
+
+ Write a script to return the max score after splitting the string into
+ two non-empty substrings. The score after splitting a string is the
+ number of zeros in the left substring plus the number of ones in the
+ right substring.
+
+Example 1
+
+Input: $str = "0011"
+Output: 4
+
+1: left = "0", right = "011" => 1 + 2 => 3
+2: left = "00", right = "11" => 2 + 2 => 4
+3: left = "001", right = "1" => 2 + 1 => 3
+
+Example 2
+
+Input: $str = "0000"
+Output: 3
+
+1: left = "0", right = "000" => 1 + 0 => 1
+2: left = "00", right = "00" => 2 + 0 => 2
+3: left = "000", right = "0" => 3 + 0 => 3
+
+Example 3
+
+Input: $str = "1111"
+Output: 3
+
+1: left = "1", right = "111" => 0 + 3 => 3
+2: left = "11", right = "11" => 0 + 2 => 2
+3: left = "111", right = "1" => 0 + 1 => 1
+
+Example 4
+
+Input: $str = "0101"
+Output: 3
+
+1: left = "0", right = "101" => 1 + 2 => 3
+2: left = "01", right = "01" => 1 + 1 => 2
+3: left = "010", right = "1" => 2 + 1 => 3
+
+Example 5
+
+Input: $str = "011101"
+Output: 5
+
+1: left = "0", right = "11101" => 1 + 4 => 5
+2: left = "01", right = "1101" => 1 + 3 => 4
+3: left = "011", right = "101" => 1 + 2 => 3
+4: left = "0111", right = "01" => 1 + 1 => 2
+5: left = "01110", right = "1" => 2 + 1 => 3
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 12th October
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def ms(string: str) -> int:
+ return max( string[:i].count("0") + string[i:].count("1") for i in range(1,len(string)) )
+ '''
+ l = len(string)
+ sm = 0
+ off = [False for i in range(l)]
+ for i in range(l):
+ if string[i] == "0":
+ sm += 1
+ else:
+ off[i] = True
+ mx = 0
+ while l > 1:
+ l -= 1
+ if off[l]:
+ sm += 1
+ else:
+ sm -= 1
+ if mx < sm:
+ mx = sm
+ return mx
+ '''
+
+import unittest
+
+class TestMs(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ "0011": 4,
+ "0000": 3,
+ "1111": 3,
+ "0101": 3,
+ "011101": 5,
+ "011111011111011111011111011111011111011111011111011111011111011111011111011111011111011111011111011111": 86,
+ }.items():
+ self.assertEqual(ms(inpt),otpt)
+
+unittest.main()