diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-30 13:50:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-30 13:50:07 +0100 |
| commit | 7edec2d1810751b703297f27426a9629eb3076d0 (patch) | |
| tree | 24faf33fbb55c59fa74b9d5a88656dc8c03374fe /challenge-275/sgreen/python | |
| parent | 6f401642435ccfb226356a1079f44ccdfc7cc00a (diff) | |
| parent | b7df036a3f3988e2ae38af4f43b8d53f4cf5e0f3 (diff) | |
| download | perlweeklychallenge-club-7edec2d1810751b703297f27426a9629eb3076d0.tar.gz perlweeklychallenge-club-7edec2d1810751b703297f27426a9629eb3076d0.tar.bz2 perlweeklychallenge-club-7edec2d1810751b703297f27426a9629eb3076d0.zip | |
Merge pull request #10341 from simongreen-net/master
sgreen solutions to challenge 275
Diffstat (limited to 'challenge-275/sgreen/python')
| -rwxr-xr-x | challenge-275/sgreen/python/ch-1.py | 40 | ||||
| -rwxr-xr-x | challenge-275/sgreen/python/ch-2.py | 41 | ||||
| -rwxr-xr-x | challenge-275/sgreen/python/test.py | 23 |
3 files changed, 104 insertions, 0 deletions
diff --git a/challenge-275/sgreen/python/ch-1.py b/challenge-275/sgreen/python/ch-1.py new file mode 100755 index 0000000000..e84c66de13 --- /dev/null +++ b/challenge-275/sgreen/python/ch-1.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +import sys + + +def broken_keys(s: str, keys: list) -> int: + """ + Counts the number of words in a string that do not contain any of the specified keys. + + Args: + s (str): The input string. + keys (list): A list of keys to check for in the words. + + Returns: + int: The count of words that do not contain any of the specified keys. + """ + + # Convert values to lowercase + words = s.lower().split(' ') + keys = [key.lower() for key in keys] + + # Initialize the counter + count = 0 + + # Loop through the word. + for word in words: + if not any(key in word for key in keys): + count += 1 + + return count + + +def main(): + # The first value is the string and the rest are the keys + result = broken_keys(sys.argv[1], sys.argv[2:]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-275/sgreen/python/ch-2.py b/challenge-275/sgreen/python/ch-2.py new file mode 100755 index 0000000000..ec4fb291d8 --- /dev/null +++ b/challenge-275/sgreen/python/ch-2.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +import string +import sys + + +def replace_digits(s: str) -> str: + """ + Replaces digits in a string with corresponding letters from the alphabet. + + Args: + s (str): The input string. + + Returns: + str: The modified string with digits replaced by letters. + """ + + current_letter = None + solution = '' + alphabet = string.ascii_lowercase + + for char in s: + if char.isdigit(): + if current_letter is None: + raise ValueError('The first number must follow a letter.') + solution += alphabet[(alphabet.index(current_letter) + + int(char)) % 26] + else: + solution += char + current_letter = char + + return solution + + +def main(): + result = replace_digits(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-275/sgreen/python/test.py b/challenge-275/sgreen/python/test.py new file mode 100755 index 0000000000..96f643528f --- /dev/null +++ b/challenge-275/sgreen/python/test.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +import unittest +ch_1 = __import__('ch-1') +ch_2 = __import__('ch-2') + + +class TestClass(unittest.TestCase): + def test_ch_1(self): + self.assertEqual(ch_1.broken_keys('Perl Weekly Challenge', ['l', 'a']), 0) + self.assertEqual(ch_1.broken_keys('Perl and Raku', ['a']), 1) + self.assertEqual(ch_1.broken_keys('Well done Team PWC', ['l', 'o']), 2) + self.assertEqual(ch_1.broken_keys('The joys of polyglottism', ['T']), 2) + + def test_ch_2(self): + self.assertEqual(ch_2.replace_digits('a1c1e1'), 'abcdef') + self.assertEqual(ch_2.replace_digits('a1b2c3d4'), 'abbdcfdh') + self.assertEqual(ch_2.replace_digits('b2b'), 'bdb') + self.assertEqual(ch_2.replace_digits('a16z'), 'abgz') + + +if __name__ == '__main__': + unittest.main() |
