diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-09-22 23:46:12 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-22 23:46:12 +0100 |
| commit | f7735f052505c50b29ee93599feafe6b999117ab (patch) | |
| tree | c7cefb5c7b83e18783a5d50708d47bc86df30f1d | |
| parent | fc54f30ddbaec72fe2bdc26aed2ca632793b4299 (diff) | |
| parent | 7100c57667e38d961da33d83fc6853aed45f8ecb (diff) | |
| download | perlweeklychallenge-club-f7735f052505c50b29ee93599feafe6b999117ab.tar.gz perlweeklychallenge-club-f7735f052505c50b29ee93599feafe6b999117ab.tar.bz2 perlweeklychallenge-club-f7735f052505c50b29ee93599feafe6b999117ab.zip | |
Merge pull request #12714 from pokgopun/pwc340
Pwc340
| -rw-r--r-- | challenge-340/pokgopun/go/ch-1.go | 118 | ||||
| -rw-r--r-- | challenge-340/pokgopun/go/ch-2.go | 90 | ||||
| -rw-r--r-- | challenge-340/pokgopun/python/ch-1.py | 90 | ||||
| -rw-r--r-- | challenge-340/pokgopun/python/ch-2.py | 79 |
4 files changed, 377 insertions, 0 deletions
diff --git a/challenge-340/pokgopun/go/ch-1.go b/challenge-340/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..652b5c093f --- /dev/null +++ b/challenge-340/pokgopun/go/ch-1.go @@ -0,0 +1,118 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-340/ +/*# + +Task 1: Duplicate Removals + +Submitted by: [48]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str, consisting of lowercase English letters. + + Write a script to return the final string after all duplicate removals + have been made. Repeat duplicate removals on the given string until we + no longer can. + + A duplicate removal consists of choosing two adjacent and equal + letters and removing them. + +Example 1 + +Input: $str = 'abbaca' +Output: 'ca' + +Step 1: Remove 'bb' => 'aaca' +Step 2: Remove 'aa' => 'ca' + +Example 2 + +Input: $str = 'azxxzy' +Output: 'ay' + +Step 1: Remove 'xx' => 'azzy' +Step 2: Remove 'zz' => 'ay' + +Example 3 + +Input: $str = 'aaaaaaaa' +Output: '' + +Step 1: Remove 'aa' => 'aaaaaa' +Step 2: Remove 'aa' => 'aaaa' +Step 3: Remove 'aa' => 'aa' +Step 4: Remove 'aa' => '' + +Example 4 + +Input: $str = 'aabccba' +Output: 'a' + +Step 1: Remove 'aa' => 'bccba' +Step 2: Remove 'cc' => 'bba' +Step 3: Remove 'bb' => 'a' + +Example 5 + +Input: $str = 'abcddcba' +Output: '' + +Step 1: Remove 'dd' => 'abccba' +Step 2: Remove 'cc' => 'abba' +Step 3: Remove 'bb' => 'aa' +Step 4: Remove 'aa' => '' + +Task 2: Ascending Numbers +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func dr(bs []byte) []byte { + l := len(bs) + if l < 2 { + return bs + } + i := 0 + for i < l-1 { + if bs[i] == bs[i+1] { + copy(bs[i:], bs[i+2:]) + l -= 2 + bs = bs[:l] + continue + } + i++ + } + return bs +} + +func DuplicateRemovals(str string) string { + bs := []byte(str) + for { + l := len(bs) + bs = dr(bs) + if len(bs) == l { + break + } + } + return string(bs) +} + +func main() { + for _, data := range []struct { + input, output string + }{ + {"abbaca", "ca"}, + {"azxxzy", "ay"}, + {"aaaaaaaa", ""}, + {"aabccba", "a"}, + {"abcddcba", ""}, + } { + io.WriteString(os.Stdout, cmp.Diff(DuplicateRemovals(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-340/pokgopun/go/ch-2.go b/challenge-340/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..af7b8a01e9 --- /dev/null +++ b/challenge-340/pokgopun/go/ch-2.go @@ -0,0 +1,90 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-340/ +/*# + +Task 2: Ascending Numbers + +Submitted by: [49]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str, is a list of tokens separated by a single + space. Every token is either a positive number consisting of digits 0-9 + with no leading zeros, or a word consisting of lowercase English + letters. + + Write a script to check if all the numbers in the given string are + strictly increasing from left to right. + +Example 1 + +Input: $str = "The cat has 3 kittens 7 toys 10 beds" +Output: true + +Numbers 3, 7, 10 - strictly increasing. + +Example 2 + +Input: $str = 'Alice bought 5 apples 2 oranges 9 bananas' +Output: false + +Example 3 + +Input: $str = 'I ran 1 mile 2 days 3 weeks 4 months' +Output: true + +Example 4 + +Input: $str = 'Bob has 10 cars 10 bikes' +Output: false + +Example 5 + +Input: $str = 'Zero is 0 one is 1 two is 2' +Output: true + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 28th September + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "regexp" + "slices" + "strconv" + + "github.com/google/go-cmp/cmp" +) + +func an(str string) bool { + re := regexp.MustCompile(`\d+`) + var s []int + for _, v := range re.FindAllStringIndex(str, -1) { + n, _ := strconv.Atoi(str[v[0]:v[1]]) + s = append(s, n) + } + l := len(s) + return slices.IsSorted(s) && len(slices.Compact(s)) == l +} + +func main() { + for _, data := range []struct { + input string + output bool + }{ + {"The cat has 3 kittens 7 toys 10 beds", true}, + {"Alice bought 5 apples 2 oranges 9 bananas", false}, + {"I ran 1 mile 2 days 3 weeks 4 months", true}, + {"Bob has 10 cars 10 bikes", false}, + {"Zero is 0 one is 1 two is 2", true}, + } { + //fmt.Println(data.input) + io.WriteString(os.Stdout, cmp.Diff(an(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-340/pokgopun/python/ch-1.py b/challenge-340/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..51bd956fe9 --- /dev/null +++ b/challenge-340/pokgopun/python/ch-1.py @@ -0,0 +1,90 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-340/ +""" + +Task 1: Duplicate Removals + +Submitted by: [48]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str, consisting of lowercase English letters. + + Write a script to return the final string after all duplicate removals + have been made. Repeat duplicate removals on the given string until we + no longer can. + + A duplicate removal consists of choosing two adjacent and equal + letters and removing them. + +Example 1 + +Input: $str = 'abbaca' +Output: 'ca' + +Step 1: Remove 'bb' => 'aaca' +Step 2: Remove 'aa' => 'ca' + +Example 2 + +Input: $str = 'azxxzy' +Output: 'ay' + +Step 1: Remove 'xx' => 'azzy' +Step 2: Remove 'zz' => 'ay' + +Example 3 + +Input: $str = 'aaaaaaaa' +Output: '' + +Step 1: Remove 'aa' => 'aaaaaa' +Step 2: Remove 'aa' => 'aaaa' +Step 3: Remove 'aa' => 'aa' +Step 4: Remove 'aa' => '' + +Example 4 + +Input: $str = 'aabccba' +Output: 'a' + +Step 1: Remove 'aa' => 'bccba' +Step 2: Remove 'cc' => 'bba' +Step 3: Remove 'bb' => 'a' + +Example 5 + +Input: $str = 'abcddcba' +Output: '' + +Step 1: Remove 'dd' => 'abccba' +Step 2: Remove 'cc' => 'abba' +Step 3: Remove 'bb' => 'aa' +Step 4: Remove 'aa' => '' + +Task 2: Ascending Numbers +""" +### solution by pokgopun@gmail.com + +import re + +def dr(string: str) -> str: + while True: + l = len(string) + string = re.sub(r'(.)\1','',string) + if l == len(string): + break + return string + +import unittest + +class TestDr(unittest.TestCase): + def test(self): + for inpt, otpt in { + 'abbaca': 'ca', + 'azxxzy': 'ay', + 'aaaaaaaa': '', + 'aabccba': 'a', + 'abcddcba': '', + }.items(): + self.assertEqual(dr(inpt),otpt) + +unittest.main() diff --git a/challenge-340/pokgopun/python/ch-2.py b/challenge-340/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..3c777c1487 --- /dev/null +++ b/challenge-340/pokgopun/python/ch-2.py @@ -0,0 +1,79 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-340/ +""" + +Task 2: Ascending Numbers + +Submitted by: [49]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str, is a list of tokens separated by a single + space. Every token is either a positive number consisting of digits 0-9 + with no leading zeros, or a word consisting of lowercase English + letters. + + Write a script to check if all the numbers in the given string are + strictly increasing from left to right. + +Example 1 + +Input: $str = "The cat has 3 kittens 7 toys 10 beds" +Output: true + +Numbers 3, 7, 10 - strictly increasing. + +Example 2 + +Input: $str = 'Alice bought 5 apples 2 oranges 9 bananas' +Output: false + +Example 3 + +Input: $str = 'I ran 1 mile 2 days 3 weeks 4 months' +Output: true + +Example 4 + +Input: $str = 'Bob has 10 cars 10 bikes' +Output: false + +Example 5 + +Input: $str = 'Zero is 0 one is 1 two is 2' +Output: true + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 28th September + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +import re + +def an(string: str) -> bool: + numbers = [ int(m.group(1)) for m in re.finditer(r'(\d+)',string) ] + if len(numbers) < 2: + return False + n = numbers[0] + for v in numbers[1:]: + if n >= v: + return False + n = v + return True + +import unittest + +class TestAn(unittest.TestCase): + def test(self): + for inpt, otpt in { + "The cat has 3 kittens 7 toys 10 beds": True, + 'Alice bought 5 apples 2 oranges 9 bananas': False, + 'I ran 1 mile 2 days 3 weeks 4 months': True, + 'Bob has 10 cars 10 bikes': False, + 'Zero is 0 one is 1 two is 2': True, + }.items(): + self.assertEqual(an(inpt), otpt) + +unittest.main() |
