diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-24 22:25:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-24 22:25:55 +0100 |
| commit | efeb4b1121b3df61e39c3ea65d82245ad0ac7359 (patch) | |
| tree | ed76064f4ef8442f74f5a42c55a3c7950fb596f5 | |
| parent | 6e57cc6b0779f09bb1bcf486a002cd8b0d38993f (diff) | |
| parent | e47302d99328b696544a2fcf1457f86aa5b3c5ab (diff) | |
| download | perlweeklychallenge-club-efeb4b1121b3df61e39c3ea65d82245ad0ac7359.tar.gz perlweeklychallenge-club-efeb4b1121b3df61e39c3ea65d82245ad0ac7359.tar.bz2 perlweeklychallenge-club-efeb4b1121b3df61e39c3ea65d82245ad0ac7359.zip | |
Merge pull request #10314 from pokgopun/pwc275
Pwc275
| -rw-r--r-- | challenge-275/pokgopun/go/ch-1.go | 84 | ||||
| -rw-r--r-- | challenge-275/pokgopun/go/ch-2.go | 88 | ||||
| -rw-r--r-- | challenge-275/pokgopun/python/ch-1.py | 59 | ||||
| -rw-r--r-- | challenge-275/pokgopun/python/ch-2.py | 91 |
4 files changed, 322 insertions, 0 deletions
diff --git a/challenge-275/pokgopun/go/ch-1.go b/challenge-275/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..c9524faa19 --- /dev/null +++ b/challenge-275/pokgopun/go/ch-1.go @@ -0,0 +1,84 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-275/ +/*# + +Task 1: Broken Keys + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a sentence, $sentence and list of broken keys @keys. + + Write a script to find out how many words can be typed fully. + +Example 1 + +Input: $sentence = "Perl Weekly Challenge", @keys = ('l', 'a') +Output: 0 + +Example 2 + +Input: $sentence = "Perl and Raku", @keys = ('a') +Output: 1 + +Only Perl since the other word two words contain 'a' and can't be typed fully. + +Example 3 + +Input: $sentence = "Well done Team PWC", @keys = ('l', 'o') +Output: 2 + +Example 4 + +Input: $sentence = "The joys of polyglottism", @keys = ('T') +Output: 2 + +Task 2: Replace Digits +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "strings" + + "github.com/google/go-cmp/cmp" +) + +type chars []rune + +func bk(st string, cs chars) int { + words := strings.Split(st, " ") + l := len(words) + n := l + var c rune + for l > 0 { + l-- + for _, c = range cs { + if c >= 'A' && c <= 'Z' { + c += 32 + } + if strings.ContainsRune(strings.ToLower(words[l]), c) { + n-- + break + } + } + } + return n +} + +func main() { + for _, data := range []struct { + sentence string + chars chars + count int + }{ + {"Perl Weekly Challenge", chars{'l', 'a'}, 0}, + {"Perl and Raku", chars{'a'}, 1}, + {"Well done Team PWC", chars{'l', 'o'}, 2}, + {"The joys of polyglottism", chars{'T'}, 2}, + } { + io.WriteString(os.Stdout, cmp.Diff(bk(data.sentence, data.chars), data.count)) // blank if ok, otherwise show the differences + } +} diff --git a/challenge-275/pokgopun/go/ch-2.go b/challenge-275/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..c8b76fbf44 --- /dev/null +++ b/challenge-275/pokgopun/go/ch-2.go @@ -0,0 +1,88 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-275/ +/*# + +Task 2: Replace Digits + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an alphanumeric string, $str, where each character is + either a letter or a digit. + + Write a script to replace each digit in the given string with the value + of the previous letter plus (digit) places. + +Example 1 + +Input: $str = 'a1c1e1' +Ouput: 'abcdef' + +shift('a', 1) => 'b' +shift('c', 1) => 'd' +shift('e', 1) => 'f' + +Example 2 + +Input: $str = 'a1b2c3d4' +Output: 'abbdcfdh' + +shift('a', 1) => 'b' +shift('b', 2) => 'd' +shift('c', 3) => 'f' +shift('d', 4) => 'h' + +Example 3 + +Input: $str = 'b2b' +Output: 'bdb' + +Example 4 + +Input: $str = 'a16z' +Output: 'abgz' + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 30th June 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func replaceDigits(str string) string { + bs := []byte(str) + var ( + b byte + i int + ) + for i = range len(bs) { + if bs[i] >= 48 && bs[i] <= 57 { + bs[i] = b + bs[i] - 48 + } else { + b = bs[i] + } + } + return string(bs) +} + +func main() { + for _, data := range []struct { + input, output string + }{ + {"a1c1e1", "abcdef"}, + {"a1b2c3d4", "abbdcfdh"}, + {"b2b", "bdb"}, + {"a16z", "abgz"}, + } { + io.WriteString(os.Stdout, cmp.Diff(replaceDigits(data.input), data.output)) // blank if ok, otherwise show the differences + } +} diff --git a/challenge-275/pokgopun/python/ch-1.py b/challenge-275/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..181a07ee0d --- /dev/null +++ b/challenge-275/pokgopun/python/ch-1.py @@ -0,0 +1,59 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-275/ +""" + +Task 1: Broken Keys + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a sentence, $sentence and list of broken keys @keys. + + Write a script to find out how many words can be typed fully. + +Example 1 + +Input: $sentence = "Perl Weekly Challenge", @keys = ('l', 'a') +Output: 0 + +Example 2 + +Input: $sentence = "Perl and Raku", @keys = ('a') +Output: 1 + +Only Perl since the other word two words contain 'a' and can't be typed fully. + +Example 3 + +Input: $sentence = "Well done Team PWC", @keys = ('l', 'o') +Output: 2 + +Example 4 + +Input: $sentence = "The joys of polyglottism", @keys = ('T') +Output: 2 + +Task 2: Replace Digits +""" +### solution by pokgopun@gmail.com + +def bk(sentence: str, keys: tuple): + keys = set(e.lower() for e in keys) + return sum( + 1 for e in sentence.split() if set(e.lower()).intersection(keys) == set() + ) + +import unittest + +class TestBk(unittest.TestCase): + def test(self): + for (sentence, keys), otpt in { + ("Perl Weekly Challenge", ('l', 'a')): 0, + ("Perl and Raku", ('a',)): 1, + ("Well done Team PWC", ('l', 'o')): 2, + ("The joys of polyglottism", ('T',)): 2, + }.items(): + self.assertEqual(bk(sentence,keys),otpt) + +unittest.main() + + diff --git a/challenge-275/pokgopun/python/ch-2.py b/challenge-275/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..c842eca8fc --- /dev/null +++ b/challenge-275/pokgopun/python/ch-2.py @@ -0,0 +1,91 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-275/ +""" + +Task 2: Replace Digits + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an alphanumeric string, $str, where each character is + either a letter or a digit. + + Write a script to replace each digit in the given string with the value + of the previous letter plus (digit) places. + +Example 1 + +Input: $str = 'a1c1e1' +Ouput: 'abcdef' + +shift('a', 1) => 'b' +shift('c', 1) => 'd' +shift('e', 1) => 'f' + +Example 2 + +Input: $str = 'a1b2c3d4' +Output: 'abbdcfdh' + +shift('a', 1) => 'b' +shift('b', 2) => 'd' +shift('c', 3) => 'f' +shift('d', 4) => 'h' + +Example 3 + +Input: $str = 'b2b' +Output: 'bdb' + +Example 4 + +Input: $str = 'a16z' +Output: 'abgz' + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 30th June 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def replaceDigits1(string: str): + r = list(string) + for i in range(len(r)): + if r[i].isalpha(): + prv = r[i] + else: + r[i] = chr(ord(prv)+int(r[i])) + return "".join(r) + +def replaceDigits2(string: str): + r = "" + for c in string: + if c.isalpha(): + prv = c + r += c + else: + r += chr(ord(prv)+int(c)) + return r + +import unittest + +class TestReplaceDigits(unittest.TestCase): + def test1(self): + for inpt,otpt in { + 'a1c1e1': 'abcdef', + 'a1b2c3d4': 'abbdcfdh', + 'b2b': 'bdb', + 'a16z': 'abgz', + }.items(): + self.assertEqual(replaceDigits1(inpt),otpt) + def test2(self): + for inpt,otpt in { + 'a1c1e1': 'abcdef', + 'a1b2c3d4': 'abbdcfdh', + 'b2b': 'bdb', + 'a16z': 'abgz', + }.items(): + self.assertEqual(replaceDigits2(inpt),otpt) + +unittest.main() |
