diff options
| author | Michael Manring <michael@manring> | 2024-06-24 18:34:30 +1000 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2024-06-24 21:09:15 +1000 |
| commit | 5129e0b9516902c4b1ed68e1061888d2d799dfc6 (patch) | |
| tree | a60065aefba1e9b6ae68671b6e81760cf4ade303 | |
| parent | e706d2548a3ce7ff5d3ae480224ee3ee6c5577de (diff) | |
| download | perlweeklychallenge-club-5129e0b9516902c4b1ed68e1061888d2d799dfc6.tar.gz perlweeklychallenge-club-5129e0b9516902c4b1ed68e1061888d2d799dfc6.tar.bz2 perlweeklychallenge-club-5129e0b9516902c4b1ed68e1061888d2d799dfc6.zip | |
pwc275 solution in python
| -rw-r--r-- | challenge-275/pokgopun/python/ch-1.py | 59 | ||||
| -rw-r--r-- | challenge-275/pokgopun/python/ch-2.py | 91 |
2 files changed, 150 insertions, 0 deletions
diff --git a/challenge-275/pokgopun/python/ch-1.py b/challenge-275/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..181a07ee0d --- /dev/null +++ b/challenge-275/pokgopun/python/ch-1.py @@ -0,0 +1,59 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-275/ +""" + +Task 1: Broken Keys + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a sentence, $sentence and list of broken keys @keys. + + Write a script to find out how many words can be typed fully. + +Example 1 + +Input: $sentence = "Perl Weekly Challenge", @keys = ('l', 'a') +Output: 0 + +Example 2 + +Input: $sentence = "Perl and Raku", @keys = ('a') +Output: 1 + +Only Perl since the other word two words contain 'a' and can't be typed fully. + +Example 3 + +Input: $sentence = "Well done Team PWC", @keys = ('l', 'o') +Output: 2 + +Example 4 + +Input: $sentence = "The joys of polyglottism", @keys = ('T') +Output: 2 + +Task 2: Replace Digits +""" +### solution by pokgopun@gmail.com + +def bk(sentence: str, keys: tuple): + keys = set(e.lower() for e in keys) + return sum( + 1 for e in sentence.split() if set(e.lower()).intersection(keys) == set() + ) + +import unittest + +class TestBk(unittest.TestCase): + def test(self): + for (sentence, keys), otpt in { + ("Perl Weekly Challenge", ('l', 'a')): 0, + ("Perl and Raku", ('a',)): 1, + ("Well done Team PWC", ('l', 'o')): 2, + ("The joys of polyglottism", ('T',)): 2, + }.items(): + self.assertEqual(bk(sentence,keys),otpt) + +unittest.main() + + diff --git a/challenge-275/pokgopun/python/ch-2.py b/challenge-275/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..c842eca8fc --- /dev/null +++ b/challenge-275/pokgopun/python/ch-2.py @@ -0,0 +1,91 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-275/ +""" + +Task 2: Replace Digits + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an alphanumeric string, $str, where each character is + either a letter or a digit. + + Write a script to replace each digit in the given string with the value + of the previous letter plus (digit) places. + +Example 1 + +Input: $str = 'a1c1e1' +Ouput: 'abcdef' + +shift('a', 1) => 'b' +shift('c', 1) => 'd' +shift('e', 1) => 'f' + +Example 2 + +Input: $str = 'a1b2c3d4' +Output: 'abbdcfdh' + +shift('a', 1) => 'b' +shift('b', 2) => 'd' +shift('c', 3) => 'f' +shift('d', 4) => 'h' + +Example 3 + +Input: $str = 'b2b' +Output: 'bdb' + +Example 4 + +Input: $str = 'a16z' +Output: 'abgz' + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 30th June 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def replaceDigits1(string: str): + r = list(string) + for i in range(len(r)): + if r[i].isalpha(): + prv = r[i] + else: + r[i] = chr(ord(prv)+int(r[i])) + return "".join(r) + +def replaceDigits2(string: str): + r = "" + for c in string: + if c.isalpha(): + prv = c + r += c + else: + r += chr(ord(prv)+int(c)) + return r + +import unittest + +class TestReplaceDigits(unittest.TestCase): + def test1(self): + for inpt,otpt in { + 'a1c1e1': 'abcdef', + 'a1b2c3d4': 'abbdcfdh', + 'b2b': 'bdb', + 'a16z': 'abgz', + }.items(): + self.assertEqual(replaceDigits1(inpt),otpt) + def test2(self): + for inpt,otpt in { + 'a1c1e1': 'abcdef', + 'a1b2c3d4': 'abbdcfdh', + 'b2b': 'bdb', + 'a16z': 'abgz', + }.items(): + self.assertEqual(replaceDigits2(inpt),otpt) + +unittest.main() |
