diff options
| -rw-r--r-- | challenge-328/pokgopun/go/ch-1.go | 90 | ||||
| -rw-r--r-- | challenge-328/pokgopun/go/ch-2.go | 93 | ||||
| -rw-r--r-- | challenge-328/pokgopun/python/ch-1.py | 71 | ||||
| -rw-r--r-- | challenge-328/pokgopun/python/ch-2.py | 67 |
4 files changed, 321 insertions, 0 deletions
diff --git a/challenge-328/pokgopun/go/ch-1.go b/challenge-328/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..1b3cc10a9f --- /dev/null +++ b/challenge-328/pokgopun/go/ch-1.go @@ -0,0 +1,90 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-328/ +/*# + +Task 1: Replace all ? + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string containing only lower case English letters and + ?. + + Write a script to replace all ? in the given string so that the string + doesn’t contain consecutive repeating characters. + +Example 1 + +Input: $str = "a?z" +Output: "abz" + +There can be many strings, one of them is "abz". +The choices are 'a' to 'z' but we can't use either 'a' or 'z' to replace the '?' +. + +Example 2 + +Input: $str = "pe?k" +Output: "peak" + +Example 3 + +Input: $str = "gra?te" +Output: "grabte" + +Task 2: Good String +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + + "github.com/google/go-cmp/cmp" +) + +type input string + +func (in input) process() string { + rs := []rune(in) + l := len(rs) + for i := range l { + var s []rune + if rs[i] == '?' { + if i > 0 { + prv := rs[i-1] + if prv != '?' { + s = append(s, prv) + } + } + if i < l-1 { + nxt := rs[i+1] + if nxt != '?' { + s = append(s, nxt) + } + } + for r := 'a'; r <= 'z'; r++ { + if !slices.Contains(s, r) { + rs[i] = r + break + } + } + } + } + return string(rs) +} + +func main() { + for _, data := range []struct { + input input + output string + }{ + {"a?z", "abz"}, + {"pe?k", "peak"}, + {"gra?te", "grabte"}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-328/pokgopun/go/ch-2.go b/challenge-328/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..246c045e33 --- /dev/null +++ b/challenge-328/pokgopun/go/ch-2.go @@ -0,0 +1,93 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-328/ +/*# + +Task 2: Good String + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string made up of lower and upper case English letters + only. + + Write a script to return the good string of the given string. A string + is called good string if it doesn’t have two adjacent same characters, + one in upper case and other is lower case. + +Example 1 + +Input: $str = "WeEeekly" +Output: "Weekly" + +We can remove either, "eE" or "Ee" to make it good. + +Example 2 + +Input: $str = "abBAdD" +Output: "" + +We remove "bB" first: "aAdD" +Then we remove "aA": "dD" +Finally remove "dD". + +Example 3 + +Input: $str = "abc" +Output: "abc" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 6th July 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type input string + +func (in input) process() string { + //fmt.Println("=>", in) + rs := []rune(in) + l := len(rs) + for l > 1 { + i := l + for { + i-- + if max(rs[i], rs[i-1])-min(rs[i], rs[i-1]) == 'a'-'A' { + //fmt.Println("removing", string([]rune{rs[i], rs[i-1]})) + copy(rs[i-1:], rs[i+1:]) + l -= 2 + rs = rs[:l] + //fmt.Println("=>", string(rs)) + break + } + if i > 1 { + continue + } + //fmt.Println("i=", i, "l=", l) + return string(rs) + } + } + return string(rs) +} + +func main() { + for _, data := range []struct { + input input + output string + }{ + {"WeEeekly", "Weekly"}, + {"abBAdD", ""}, + {"abc", "abc"}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-328/pokgopun/python/ch-1.py b/challenge-328/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..442de91ec6 --- /dev/null +++ b/challenge-328/pokgopun/python/ch-1.py @@ -0,0 +1,71 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-328/ +""" + +Task 1: Replace all ? + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string containing only lower case English letters and + ?. + + Write a script to replace all ? in the given string so that the string + doesn’t contain consecutive repeating characters. + +Example 1 + +Input: $str = "a?z" +Output: "abz" + +There can be many strings, one of them is "abz". +The choices are 'a' to 'z' but we can't use either 'a' or 'z' to replace the '?' +. + +Example 2 + +Input: $str = "pe?k" +Output: "peak" + +Example 3 + +Input: $str = "gra?te" +Output: "grabte" + +Task 2: Good String +""" +### solution by pokgopun@gmail.com + +def ra(string: str) -> str: + chrs = list(string) + l = len(chrs) + first = ord('a') + last = ord('z') + for i in range(l): + if chrs[i] == '?': + lst: list[str] = [] + if i > 0: + prv = chrs[i-1] + if prv.isalpha(): + lst.append(ord(prv)) + if i < l - 1: + nxt = chrs[i+1] + if nxt.isalpha(): + lst.append(ord(nxt)) + for j in range(first,last+1): + if j not in lst: + chrs[i] = chr(j) + break + return "".join(chrs) + +import unittest + +class TestRa(unittest.TestCase): + def test(self): + for inpt, otpt in { + "a?z": "abz", + "pe?k": "peak", + "gra?te": "grabte", + }.items(): + self.assertEqual(ra(inpt),otpt) + +unittest.main() diff --git a/challenge-328/pokgopun/python/ch-2.py b/challenge-328/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..11020d69a1 --- /dev/null +++ b/challenge-328/pokgopun/python/ch-2.py @@ -0,0 +1,67 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-328/ +""" + +Task 2: Good String + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string made up of lower and upper case English letters + only. + + Write a script to return the good string of the given string. A string + is called good string if it doesn’t have two adjacent same characters, + one in upper case and other is lower case. + +Example 1 + +Input: $str = "WeEeekly" +Output: "Weekly" + +We can remove either, "eE" or "Ee" to make it good. + +Example 2 + +Input: $str = "abBAdD" +Output: "" + +We remove "bB" first: "aAdD" +Then we remove "aA": "dD" +Finally remove "dD". + +Example 3 + +Input: $str = "abc" +Output: "abc" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 6th July 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def gs(string: str) -> str: + chrs = list(string) + while True: + for i in range(1,len(chrs)): + if abs(ord(chrs[i])-ord(chrs[i-1])) == 32: + chrs = chrs[:i-1] + chrs[i+1:] + break + else: + break + return "".join(chrs) + +import unittest + +class TestGs(unittest.TestCase): + def test(self): + for inpt, otpt in { + "WeEeekly": "Weekly", + "abBAdD": "", + "abc": "abc", + }.items(): + self.assertEqual(gs(inpt),otpt) + +unittest.main() |
