diff options
| author | Pok <pok@goyangi> | 2025-07-21 17:22:32 +1000 |
|---|---|---|
| committer | Pok <pok@goyangi> | 2025-07-21 20:45:44 +1000 |
| commit | 9db22599e35fbde3c6c48637333b2167d1914e2f (patch) | |
| tree | 4576461c5c9119f48626437ef8d2d2a0270a2462 | |
| parent | e0f7e800fc8f1b2cb50896852a0a3ed61980f510 (diff) | |
| download | perlweeklychallenge-club-9db22599e35fbde3c6c48637333b2167d1914e2f.tar.gz perlweeklychallenge-club-9db22599e35fbde3c6c48637333b2167d1914e2f.tar.bz2 perlweeklychallenge-club-9db22599e35fbde3c6c48637333b2167d1914e2f.zip | |
pwc331 solution in python
| -rw-r--r-- | challenge-331/pokgopun/python/ch-1.py | 49 | ||||
| -rw-r--r-- | challenge-331/pokgopun/python/ch-2.py | 81 |
2 files changed, 130 insertions, 0 deletions
diff --git a/challenge-331/pokgopun/python/ch-1.py b/challenge-331/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..05f3f308df --- /dev/null +++ b/challenge-331/pokgopun/python/ch-1.py @@ -0,0 +1,49 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-331/ +""" + +Task 1: Last Word + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string. + + Write a script to find the length of last word in the given string. + +Example 1 + +Input: $str = "The Weekly Challenge" +Output: 9 + +Example 2 + +Input: $str = " Hello World " +Output: 5 + +Example 3 + +Input: $str = "Let's begin the fun" +Output: 3 + +Task 2: Buddy Strings +""" +### solution by pokgopun@gmail.com + +import re + +def lw(string: str) -> str: + m = re.search(r'(\S+)\s*$', string) + return m.end(1) - m.start(1) + +import unittest + +class TestLw(unittest.TestCase): + def test(self): + for inpt, otpt in { + "The Weekly Challenge": 9, + " Hello World ": 5, + "Let's begin the fun": 3, + }.items(): + self.assertEqual(lw(inpt),otpt) + +unittest.main() diff --git a/challenge-331/pokgopun/python/ch-2.py b/challenge-331/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..22c61d9240 --- /dev/null +++ b/challenge-331/pokgopun/python/ch-2.py @@ -0,0 +1,81 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-331/ +""" + +Task 2: Buddy Strings + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two strings, source and target. + + Write a script to find out if the given strings are Buddy Strings. +If swapping of a letter in one string make them same as the other then they are +`Buddy Strings`. + +Example 1 + +Input: $source = "fuck" + $target = "fcuk" +Output: true + +The swapping of 'u' with 'c' makes it buddy strings. + +Example 2 + +Input: $source = "love" + $target = "love" +Output: false + +Example 3 + +Input: $source = "fodo" + $target = "food" +Output: true + +Example 4 + +Input: $source = "feed" + $target = "feed" +Output: true + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 27th July 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def bs(s: str, t: str) -> bool: + l = len(s) + if l != len(t): + return False + if s == t: + if l != len(set(s)): + return True + else: + return False + lst: list[str] = [] + for i in range(l): + a, b = s[i], t[i] + if a != b: + lst.append(a) + lst.append(b) + if len(lst) == 4: + if lst[0] == lst[3] and lst[1] == lst[2]: + return s[i+1:] == t[i+1:] + return False + +import unittest + +class TestBs(unittest.TestCase): + def test(self): + for (source, target), otpt in { + ("fuck", "fcuk"): True, + ("love", "love"): False, + ("fodo", "food"): True, + ("feed", "feed"): True, + }.items(): + self.assertEqual(bs(source,target),otpt) + +unittest.main() |
