diff options
| -rw-r--r-- | challenge-255/pokgopun/python/ch-1.py | 49 | ||||
| -rw-r--r-- | challenge-255/pokgopun/python/ch-2.py | 61 |
2 files changed, 110 insertions, 0 deletions
diff --git a/challenge-255/pokgopun/python/ch-1.py b/challenge-255/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..c48ab19023 --- /dev/null +++ b/challenge-255/pokgopun/python/ch-1.py @@ -0,0 +1,49 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-255/ +""" + +Task 1: Odd Character + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two strings, $s and $t. The string $t is generated using + the shuffled characters of the string $s with an additional character. + + Write a script to find the additional character in the string $t.. + +Example 1 + +Input: $s = "Perl" $t = "Preel" +Output: "e" + +Example 2 + +Input: $s = "Weekly" $t = "Weeakly" +Output: "a" + +Example 3 + +Input: $s = "Box" $t = "Boxy" +Output: "y" + +Task 2: Most Frequent Word +""" +### solution by pokgopun@gmail.com + +def oddChar(s,t: str): + for c in s: + t = t.replace(c,"",1) + return t + +import unittest + +class TestOddChar(unittest.TestCase): + def test(self): + for (s,t), otpt in { + ("Perl","Preel"): "e", + ("Weekly","Weeakly"): "a", + ("Box","Boxy"): "y", + }.items(): + self.assertEqual(oddChar(s,t), otpt) + +unittest.main() diff --git a/challenge-255/pokgopun/python/ch-2.py b/challenge-255/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..8a936ca3f2 --- /dev/null +++ b/challenge-255/pokgopun/python/ch-2.py @@ -0,0 +1,61 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-255/ +""" + +Task 2: Most Frequent Word + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a paragraph $p and a banned word $w. + + Write a script to return the most frequent word that is not banned. + +Example 1 + +Input: $p = "Joe hit a ball, the hit ball flew far after it was hit." + $w = "hit" +Output: "ball" + +The banned word "hit" occurs 3 times. +The other word "ball" occurs 2 times. + +Example 2 + +Input: $p = "Perl and Raku belong to the same family. Perl is the most popular l +anguage in the weekly challenge." + $w = "the" +Output: "Perl" + +The banned word "the" occurs 3 times. +The other word "Perl" occurs 2 times. + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 11th February + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +import re + +def mostFreqWord(p,w: str): + dct = dict() + for s in ( e.group(0) for e in re.finditer(r'\w+',p) if e.group(0) != w ): + dct[s] = dct.setdefault(s,0) + 1 + mx = max(dct.values()) + for e in dct.items(): + if e[1] == mx: return e[0] + +import unittest + +class TestMostFreqWord(unittest.TestCase): + def test(self): + for (p,w), otpt in { + ("Joe hit a ball, the hit ball flew far after it was hit.","hit"): "ball", + ("Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.","the"): "Perl", + }.items(): + self.assertEqual(mostFreqWord(p,w),otpt) + +unittest.main() |
