diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-11 13:01:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-11 13:01:38 +0100 |
| commit | 589a5e7004cb3b176fe0ad3dba9590aa2130372b (patch) | |
| tree | 9b7084d2b048d7e39eeb09b29f69d601bab61911 | |
| parent | a401fc68daf6e91688fce5fe8707f7b82e98187f (diff) | |
| parent | 7f4455ab3e39bd208a6a1f6449c2d702f19df091 (diff) | |
| download | perlweeklychallenge-club-589a5e7004cb3b176fe0ad3dba9590aa2130372b.tar.gz perlweeklychallenge-club-589a5e7004cb3b176fe0ad3dba9590aa2130372b.tar.bz2 perlweeklychallenge-club-589a5e7004cb3b176fe0ad3dba9590aa2130372b.zip | |
Merge pull request #10248 from pokgopun/pwc273
Pwc273
| -rw-r--r-- | challenge-273/pokgopun/go/ch-1.go | 86 | ||||
| -rw-r--r-- | challenge-273/pokgopun/go/ch-2.go | 77 | ||||
| -rw-r--r-- | challenge-273/pokgopun/python/ch-1.py | 66 | ||||
| -rw-r--r-- | challenge-273/pokgopun/python/ch-2.py | 60 |
4 files changed, 289 insertions, 0 deletions
diff --git a/challenge-273/pokgopun/go/ch-1.go b/challenge-273/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..5e9355844b --- /dev/null +++ b/challenge-273/pokgopun/go/ch-1.go @@ -0,0 +1,86 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-273/ +/*# + +Task 1: Percentage of Character + +Submitted by: [52]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str and a character $char. + + Write a script to return the percentage, nearest whole, of given + character in the given string. + +Example 1 + +Input: $str = "perl", $char = "e" +Output: 25 + +Example 2 + +Input: $str = "java", $char = "a" +Output: 50 + +Example 3 + +Input: $str = "python", $char = "m" +Output: 0 + +Example 4 + +Input: $str = "ada", $char = "a" +Output: 67 + +Example 5 + +Input: $str = "ballerina", $char = "l" +Output: 22 + +Example 6 + +Input: $str = "analitik", $char = "k" +Output: 13 + +Task 2: B After A +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func poc(str string, chr rune) int { + res := 0 + for _, v := range str { + if v == chr { + res++ + } + } + res = 1000 * res / len(str) + if res%10 < 5 { + return res / 10 + } + return 1 + res/10 +} + +func main() { + for _, data := range []struct { + str string + chr rune + p int + }{ + {"perl", 'e', 25}, + {"java", 'a', 50}, + {"python", 'm', 0}, + {"ada", 'a', 67}, + {"ballerina", 'l', 22}, + {"analitik", 'k', 13}, + } { + io.WriteString(os.Stdout, cmp.Diff(poc(data.str, data.chr), data.p)) // blank if ok, otherwise show the differences + } +} diff --git a/challenge-273/pokgopun/go/ch-2.go b/challenge-273/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..79ba8a20ff --- /dev/null +++ b/challenge-273/pokgopun/go/ch-2.go @@ -0,0 +1,77 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-273/ +/*# + +Task 2: B After A + +Submitted by: [53]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str. + + Write a script to return true if there is at least one b, and no a + appears after the first b. + +Example 1 + +Input: $str = "aabb" +Output: true + +Example 2 + +Input: $str = "abab" +Output: false + +Example 3 + +Input: $str = "aaa" +Output: false + +Example 4 + +Input: $str = "bbb" +Output: true + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 16th June 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func aAfterB(str string) bool { + for i, v := range str { + if v == 'b' { + for _, c := range str[i+1:] { + if c == 'a' { + return false + } + return true + } + } + } + return false +} + +func main() { + for _, data := range []struct { + str string + res bool + }{ + {"aabb", true}, + {"abab", false}, + {"aaa", false}, + {"bbb", true}, + } { + io.WriteString(os.Stdout, cmp.Diff(aAfterB(data.str), data.res)) // blank if ok, otherwise show the differences + } +} diff --git a/challenge-273/pokgopun/python/ch-1.py b/challenge-273/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..a6194a7d01 --- /dev/null +++ b/challenge-273/pokgopun/python/ch-1.py @@ -0,0 +1,66 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-273/ +""" + +Task 1: Percentage of Character + +Submitted by: [52]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str and a character $char. + + Write a script to return the percentage, nearest whole, of given + character in the given string. + +Example 1 + +Input: $str = "perl", $char = "e" +Output: 25 + +Example 2 + +Input: $str = "java", $char = "a" +Output: 50 + +Example 3 + +Input: $str = "python", $char = "m" +Output: 0 + +Example 4 + +Input: $str = "ada", $char = "a" +Output: 67 + +Example 5 + +Input: $str = "ballerina", $char = "l" +Output: 22 + +Example 6 + +Input: $str = "analitik", $char = "k" +Output: 13 + +Task 2: B After A +""" +### solution by pokgopun@gmail.com + +def poc(string, char): + res = 1000 * string.count(char) / len(string) + return res // 10 if res % 10 < 5 else 1 + res // 10 + +import unittest + +class TestPoc(unittest.TestCase): + def test(self): + for (string, char), otpt in { + ("perl", "e"): 25, + ("java", "a"): 50, + ("python", "m"): 0, + ("ada", "a"): 67, + ("ballerina", "l"): 22, + ("analitik", "k"): 13, + }.items(): + self.assertEqual(poc(string, char), otpt) + +unittest.main() diff --git a/challenge-273/pokgopun/python/ch-2.py b/challenge-273/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..060fe3686e --- /dev/null +++ b/challenge-273/pokgopun/python/ch-2.py @@ -0,0 +1,60 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-273/ +""" + +Task 2: B After A + +Submitted by: [53]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str. + + Write a script to return true if there is at least one b, and no a + appears after the first b. + +Example 1 + +Input: $str = "aabb" +Output: true + +Example 2 + +Input: $str = "abab" +Output: false + +Example 3 + +Input: $str = "aaa" +Output: false + +Example 4 + +Input: $str = "bbb" +Output: true + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 16th June 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def bAfterA(string): + i = string.find("b") + if i == -1: + return False + return string[i+1:].find("a") == -1 + +import unittest + +class TestBAfterA(unittest.TestCase): + def test(self): + for string, otpt in { + "aabb": True, + "abab": False, + "aaa": False, + "bbb": True, + }.items(): + self.assertEqual(bAfterA(string),otpt) + +unittest.main() |
