diff options
| -rw-r--r-- | challenge-341/pokgopun/go/ch-1.go | 88 | ||||
| -rw-r--r-- | challenge-341/pokgopun/go/ch-2.go | 86 | ||||
| -rw-r--r-- | challenge-341/pokgopun/python/ch-1.py | 78 | ||||
| -rw-r--r-- | challenge-341/pokgopun/python/ch-2.py | 70 |
4 files changed, 322 insertions, 0 deletions
diff --git a/challenge-341/pokgopun/go/ch-1.go b/challenge-341/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..14a99c38f8 --- /dev/null +++ b/challenge-341/pokgopun/go/ch-1.go @@ -0,0 +1,88 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-341/ +/*# + +Task 1: Broken Keyboard + +Submitted by: [39]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string containing English letters only and also you are + given broken keys. + + Write a script to return the total words in the given sentence can be + typed completely. + +Example 1 + +Input: $str = 'Hello World', @keys = ('d') +Output: 1 + +With broken key 'd', we can only type the word 'Hello'. + +Example 2 + +Input: $str = 'apple banana cherry', @keys = ('a', 'e') +Output: 0 + +Example 3 + +Input: $str = 'Coding is fun', @keys = () +Output: 3 + +No keys broken. + +Example 4 + +Input: $str = 'The Weekly Challenge', @keys = ('a','b') +Output: 2 + +Example 5 + +Input: $str = 'Perl and Python', @keys = ('p') +Output: 1 + +Task 2: Reverse Prefix +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "strings" + + "github.com/google/go-cmp/cmp" +) + +func bk(str string, keys []rune) int { + l := len(keys) + var cnt, i int + for s := range strings.SplitSeq(strings.ToLower(str), " ") { + for i = 0; i < l; i++ { + if strings.ContainsRune(s, keys[i]) { + break + } + } + if i == l { + cnt++ + } + } + return cnt +} + +func main() { + for _, data := range []struct { + str string + keys []rune + output int + }{ + {"Hello World", []rune{'d'}, 1}, + {"apple banana cherry", []rune{'a', 'e'}, 0}, + {"Coding is fun", []rune{}, 3}, + {"The Weekly Challenge", []rune{'a', 'b'}, 2}, + {"Perl and Python", []rune{'p'}, 1}, + } { + io.WriteString(os.Stdout, cmp.Diff(bk(data.str, data.keys), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-341/pokgopun/go/ch-2.go b/challenge-341/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..3a43c95343 --- /dev/null +++ b/challenge-341/pokgopun/go/ch-2.go @@ -0,0 +1,86 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-341/ +/*# + +Task 2: Reverse Prefix + +Submitted by: [40]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str and a character in the given string, + $char. + + Write a script to reverse the prefix upto the first occurrence of the + given $char in the given string $str and return the new string. + +Example 1 + +Input: $str = "programming", $char = "g" +Output: "gorpramming" + +Reverse of prefix "prog" is "gorp". + +Example 2 + +Input: $str = "hello", $char = "h" +Output: "hello" + +Example 3 + +Input: $str = "abcdefghij", $char = "h" +Output: "hgfedcbaij" + +Example 4 + +Input: $str = "reverse", $char = "s" +Output: "srevere" + +Example 5 + +Input: $str = "perl", $char = "r" +Output: "repl" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 5th October + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + "strings" + + "github.com/google/go-cmp/cmp" +) + +func rp(s string, r rune) string { + i := strings.IndexRune(s, r) + if i < 1 { + return s + } + prfx := []rune(s[:i+1]) + slices.Reverse(prfx) + return string(prfx) + s[i+1:] +} + +func main() { + for _, data := range []struct { + str string + char rune + output string + }{ + {"programming", 'g', "gorpramming"}, + {"hello", 'h', "hello"}, + {"abcdefghij", 'h', "hgfedcbaij"}, + {"reverse", 's', "srevere"}, + {"perl", 'r', "repl"}, + } { + io.WriteString(os.Stdout, cmp.Diff(rp(data.str, data.char), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-341/pokgopun/python/ch-1.py b/challenge-341/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..94628e9a44 --- /dev/null +++ b/challenge-341/pokgopun/python/ch-1.py @@ -0,0 +1,78 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-341/ +""" + +Task 1: Broken Keyboard + +Submitted by: [39]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string containing English letters only and also you are + given broken keys. + + Write a script to return the total words in the given sentence can be + typed completely. + +Example 1 + +Input: $str = 'Hello World', @keys = ('d') +Output: 1 + +With broken key 'd', we can only type the word 'Hello'. + +Example 2 + +Input: $str = 'apple banana cherry', @keys = ('a', 'e') +Output: 0 + +Example 3 + +Input: $str = 'Coding is fun', @keys = () +Output: 3 + +No keys broken. + +Example 4 + +Input: $str = 'The Weekly Challenge', @keys = ('a','b') +Output: 2 + +Example 5 + +Input: $str = 'Perl and Python', @keys = ('p') +Output: 1 + +Task 2: Reverse Prefix +""" +### solution by pokgopun@gmail.com + +def bk(string: str, keys: tuple[str]) -> int: + cnt = 0 + string = string.lower() + for w in string.split(" "): + for k in keys: + #print(w,k) + if w.find(k) != -1: + break + else: + cnt += 1 + return cnt + +def bk0(string: str, keys: tuple[str]) -> int: + keys = set(keys) + return sum( 1 if keys.intersection(set(e.lower())) == set() else 0 for e in string.split(" ") ) + +import unittest + +class TestBk(unittest.TestCase): + def test(self): + for (string, keys), otpt in { + ('Hello World', ('d')): 1, + ('apple banana cherry', ('a', 'e')): 0, + ('Coding is fun', ()): 3, + ('The Weekly Challenge', ('a','b')): 2, + ('Perl and Python', ('p')): 1, + }.items(): + #print(string,keys) + self.assertEqual(bk(string,keys),otpt) + +unittest.main() diff --git a/challenge-341/pokgopun/python/ch-2.py b/challenge-341/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..70eb41600f --- /dev/null +++ b/challenge-341/pokgopun/python/ch-2.py @@ -0,0 +1,70 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-341/ +""" + +Task 2: Reverse Prefix + +Submitted by: [40]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str and a character in the given string, + $char. + + Write a script to reverse the prefix upto the first occurrence of the + given $char in the given string $str and return the new string. + +Example 1 + +Input: $str = "programming", $char = "g" +Output: "gorpramming" + +Reverse of prefix "prog" is "gorp". + +Example 2 + +Input: $str = "hello", $char = "h" +Output: "hello" + +Example 3 + +Input: $str = "abcdefghij", $char = "h" +Output: "hgfedcbaij" + +Example 4 + +Input: $str = "reverse", $char = "s" +Output: "srevere" + +Example 5 + +Input: $str = "perl", $char = "r" +Output: "repl" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 5th October + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def rp(string: str, char: str) -> str: + i = string.find(char) + if i < 1: + return string + return "".join(reversed(string[:i+1])) + string[i+1:] + +import unittest + +class TestRp(unittest.TestCase): + def test(self): + for (string,char), otpt in { + ("programming", "g"): "gorpramming", + ("hello", "h"): "hello", + ("abcdefghij", "h"): "hgfedcbaij", + ("reverse", "s"): "srevere", + ("perl", "r"): "repl", + }.items(): + self.assertEqual(rp(string,char),otpt) + +unittest.main() |
