diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-22 12:52:51 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-22 12:52:51 +0100 |
| commit | 95ff536e894a177444ac5cac0da499c9951f443f (patch) | |
| tree | 3ed0452da08231834d216cbf94f78a7445631e31 /challenge-331/sgreen/python | |
| parent | b335afba876ef25a29b33491c40bb799259b739f (diff) | |
| parent | a1e3158274222306a52771654c65396d9cfc4788 (diff) | |
| download | perlweeklychallenge-club-95ff536e894a177444ac5cac0da499c9951f443f.tar.gz perlweeklychallenge-club-95ff536e894a177444ac5cac0da499c9951f443f.tar.bz2 perlweeklychallenge-club-95ff536e894a177444ac5cac0da499c9951f443f.zip | |
Merge pull request #12397 from simongreen-net/master
sgreen solutions to challenge 331
Diffstat (limited to 'challenge-331/sgreen/python')
| -rwxr-xr-x | challenge-331/sgreen/python/ch-1.py | 23 | ||||
| -rwxr-xr-x | challenge-331/sgreen/python/ch-2.py | 29 | ||||
| -rwxr-xr-x | challenge-331/sgreen/python/test.py | 23 |
3 files changed, 75 insertions, 0 deletions
diff --git a/challenge-331/sgreen/python/ch-1.py b/challenge-331/sgreen/python/ch-1.py new file mode 100755 index 0000000000..272f25d770 --- /dev/null +++ b/challenge-331/sgreen/python/ch-1.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def last_word(input_string: str) -> int: + # Find the last word in the input string and return its length + match = re.search( + r"([a-z'-]*[a-z])[^a-z]*$", + input_string.strip(), + re.IGNORECASE + ) + return len(match.group(1)) if match else 0 + + +def main(): + result = last_word(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-331/sgreen/python/ch-2.py b/challenge-331/sgreen/python/ch-2.py new file mode 100755 index 0000000000..dc63e17945 --- /dev/null +++ b/challenge-331/sgreen/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import sys + +from itertools import combinations + +def buddy_strings(source: str, target: str) -> bool: + # Strings must be of the same length + if len(source) != len(target): + return False + + for i, j in combinations(range(len(source)), 2): + # Swap characters at positions i and j in source + swapped = list(source) + swapped[i], swapped[j] = swapped[j], swapped[i] + # Check if the swapped string matches the target + if ''.join(swapped) == target: + return True + + return False + + +def main(): + result = buddy_strings(sys.argv[1], sys.argv[2]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-331/sgreen/python/test.py b/challenge-331/sgreen/python/test.py new file mode 100755 index 0000000000..636ba07a77 --- /dev/null +++ b/challenge-331/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.last_word("The Weekly Challenge"), 9) + self.assertEqual(ch_1.last_word("The Weekly Challenge!"), 9) + self.assertEqual(ch_1.last_word(" Hello World "), 5) + self.assertEqual(ch_1.last_word("Let's"), 5) + self.assertEqual(ch_1.last_word("!!!"), 0) + + def test_ch_2(self): + self.assertTrue(ch_2.buddy_strings("nice", "ncie")) + self.assertFalse(ch_2.buddy_strings("love", "love")) + self.assertTrue(ch_2.buddy_strings("feed", "feed")) + + +if __name__ == '__main__': + unittest.main() |
