diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-06 23:06:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-06 23:06:42 +0100 |
| commit | 259e0413ae2d77dd21017d6ed5f1fc8bfaf98937 (patch) | |
| tree | a08774ba3411e016a6d39046ebd0f3de1807726c /challenge-328/sgreen/python | |
| parent | 8d230dd8f7bc2fbf026029fab1c38b7179f7a8d4 (diff) | |
| parent | 517928ee2af6c019d908776d7a2b4fe4107b529d (diff) | |
| download | perlweeklychallenge-club-259e0413ae2d77dd21017d6ed5f1fc8bfaf98937.tar.gz perlweeklychallenge-club-259e0413ae2d77dd21017d6ed5f1fc8bfaf98937.tar.bz2 perlweeklychallenge-club-259e0413ae2d77dd21017d6ed5f1fc8bfaf98937.zip | |
Merge pull request #12292 from simongreen-net/master
sgreen solutions to challenge 328
Diffstat (limited to 'challenge-328/sgreen/python')
| -rwxr-xr-x | challenge-328/sgreen/python/ch-1.py | 37 | ||||
| -rwxr-xr-x | challenge-328/sgreen/python/ch-2.py | 29 | ||||
| -rwxr-xr-x | challenge-328/sgreen/python/test.py | 22 |
3 files changed, 88 insertions, 0 deletions
diff --git a/challenge-328/sgreen/python/ch-1.py b/challenge-328/sgreen/python/ch-1.py new file mode 100755 index 0000000000..16e539b9b1 --- /dev/null +++ b/challenge-328/sgreen/python/ch-1.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +import sys + + +def replace_all_questions(input_string: str) -> str: + solution = '' + for idx, char in enumerate(input_string): + if char != '?': + solution += char + continue + + # Get the surrounding characters + letters = [] + if idx > 0: + letters.append(input_string[idx - 1]) + if idx < len(input_string) - 1: + letters.append(input_string[idx + 1]) + + # Replace '?' with 'a', 'b', or 'c' based on surrounding characters + if 'a' not in letters: + solution += 'a' + elif 'b' not in letters: + solution += 'b' + else: + solution += 'c' + + return solution + + +def main(): + result = replace_all_questions(sys.argv[1]) + print('"' + result + '"') + + +if __name__ == '__main__': + main() diff --git a/challenge-328/sgreen/python/ch-2.py b/challenge-328/sgreen/python/ch-2.py new file mode 100755 index 0000000000..dff805c0b4 --- /dev/null +++ b/challenge-328/sgreen/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import sys + + +def good_string(input_string: str) -> str: + solution = input_string + while True: + for idx in range(0, len(solution)-1): + char = solution[idx] + # If the next two letters are the same but different case, remove them + if ((char.isupper() and solution[idx + 1] == char.lower()) or + (char.islower() and solution[idx + 1] == char.upper())): + solution = solution[:idx] + solution[idx + 2:] + break + else: + # No pairs were removed + break + + return solution + + +def main(): + result = good_string(sys.argv[1]) + print('"' + result + '"') + + +if __name__ == '__main__': + main() diff --git a/challenge-328/sgreen/python/test.py b/challenge-328/sgreen/python/test.py new file mode 100755 index 0000000000..3e9e91f5d9 --- /dev/null +++ b/challenge-328/sgreen/python/test.py @@ -0,0 +1,22 @@ +#!/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.replace_all_questions('a?z'), 'abz') + self.assertEqual(ch_1.replace_all_questions('pe?k'), 'peak') + self.assertEqual(ch_1.replace_all_questions('gra?te'), 'grabte') + self.assertEqual(ch_1.replace_all_questions('gra?be'), 'gracbe') + + def test_ch_2(self): + self.assertEqual(ch_2.good_string('WeEeekly'), 'Weekly') + self.assertEqual(ch_2.good_string('abBAdD'), '') + self.assertEqual(ch_2.good_string('abc'), 'abc') + + +if __name__ == '__main__': + unittest.main() |
