diff options
| author | Michael Manring <michael@manring> | 2024-06-11 17:45:40 +1000 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2024-06-11 18:16:46 +1000 |
| commit | 292b6158c007e8600f9fdefc113e52f3a14f21dc (patch) | |
| tree | 410a80d44360d2d20b34929c9526bff59c01a751 | |
| parent | 514530d6be7cc5067a95a11ebedff9e0d4d46cfe (diff) | |
| download | perlweeklychallenge-club-292b6158c007e8600f9fdefc113e52f3a14f21dc.tar.gz perlweeklychallenge-club-292b6158c007e8600f9fdefc113e52f3a14f21dc.tar.bz2 perlweeklychallenge-club-292b6158c007e8600f9fdefc113e52f3a14f21dc.zip | |
pwc273 solution in python
| -rw-r--r-- | challenge-273/pokgopun/python/ch-1.py | 66 | ||||
| -rw-r--r-- | challenge-273/pokgopun/python/ch-2.py | 60 |
2 files changed, 126 insertions, 0 deletions
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() |
