diff options
| -rw-r--r-- | challenge-280/pokgopun/python/ch-1.py | 53 | ||||
| -rw-r--r-- | challenge-280/pokgopun/python/ch-2.py | 67 |
2 files changed, 120 insertions, 0 deletions
diff --git a/challenge-280/pokgopun/python/ch-1.py b/challenge-280/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..f3f4684f8e --- /dev/null +++ b/challenge-280/pokgopun/python/ch-1.py @@ -0,0 +1,53 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-280/ +""" + +Task 1: Twice Appearance + +Submitted by: [53]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str, containing lowercase English letters + only. + + Write a script to print the first letter that appears twice. + +Example 1 + +Input: $str = "acbddbca" +Output: "d" + +Example 2 + +Input: $str = "abccd" +Output: "c" + +Example 3 + +Input: $str = "abcdabbb" +Output: "a" + +Task 2: Count Asterisks +""" +### solution by pokgopun@gmail.com + +def ta(string: str): + return min( + e for e in ( + (string[t[0]+1:].find(t[1]),t[1]) for t in ( + (string.find(c),c) for c in set(string) + ) + ) if e[0] >= 0 + )[1] + +import unittest + +class TestTa(unittest.TestCase): + def test(self): + for inpt,otpt in { + # "acbddbca": "d", + "abccd": "c", + "abcdabbb": "a", + }.items(): + self.assertEqual(ta(inpt),otpt) + +unittest.main() diff --git a/challenge-280/pokgopun/python/ch-2.py b/challenge-280/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..ce4a624900 --- /dev/null +++ b/challenge-280/pokgopun/python/ch-2.py @@ -0,0 +1,67 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-280/ +""" + +Task 2: Count Asterisks + +Submitted by: [54]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str, where every two consecutive vertical bars + are grouped into a pair. + + Write a script to return the number of asterisks, *, excluding any + between each pair of vertical bars. + +Example 1 + +Input: $str = "p|*e*rl|w**e|*ekly|" +Ouput: 2 + +The characters we are looking here are "p" and "w**e". + +Example 2 + +Input: $str = "perl" +Ouput: 0 + +Example 3 + +Input: $str = "th|ewe|e**|k|l***ych|alleng|e" +Ouput: 5 + +The characters we are looking here are "th", "e**", "l***ych" and "e". + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 4th August + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def cntAtr(string:str): + c = 0 + l = len(string) + while l > 0: + l -= 1 + if string[l] == "*": + c += 1 + elif string[l] == "|": + i = string[:l].rfind("|") + if i != -1: + l = i + return c + +import unittest + +class TestCntAtr(unittest.TestCase): + def test(self): + for inpt, otpt in { + "p|*e*rl|w**e|*ekly|": 2, + "perl": 0, + "th|ewe|e**|k|l***ych|alleng|e": 5, + }.items(): + self.assertEqual(cntAtr(inpt),otpt) + +unittest.main() |
