From aea2dde2ff05366db4b8560d08a4a80b028e9d66 Mon Sep 17 00:00:00 2001 From: Pok Date: Mon, 29 Sep 2025 09:54:42 +0700 Subject: pwc341 solution in python --- challenge-341/pokgopun/python/ch-1.py | 74 +++++++++++++++++++++++++++++++++++ challenge-341/pokgopun/python/ch-2.py | 71 +++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 challenge-341/pokgopun/python/ch-1.py create mode 100644 challenge-341/pokgopun/python/ch-2.py diff --git a/challenge-341/pokgopun/python/ch-1.py b/challenge-341/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..0987822638 --- /dev/null +++ b/challenge-341/pokgopun/python/ch-1.py @@ -0,0 +1,74 @@ +### 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 + +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..621c8b3776 --- /dev/null +++ b/challenge-341/pokgopun/python/ch-2.py @@ -0,0 +1,71 @@ +### 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: + string + 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() -- cgit