diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-04-20 09:37:20 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-20 09:37:20 +0100 |
| commit | f14d0fd8742f682db99c2ee0a8ebd5b6fb48e6bb (patch) | |
| tree | c400aacfc11947c486058ee87cf524adacb27042 /challenge-317/sgreen/python | |
| parent | e18c8e6fcc13c8d06da8e05e1f1226d61b9aade1 (diff) | |
| parent | 2f9ac991c97bb4de0a76c631290a194c6377d241 (diff) | |
| download | perlweeklychallenge-club-f14d0fd8742f682db99c2ee0a8ebd5b6fb48e6bb.tar.gz perlweeklychallenge-club-f14d0fd8742f682db99c2ee0a8ebd5b6fb48e6bb.tar.bz2 perlweeklychallenge-club-f14d0fd8742f682db99c2ee0a8ebd5b6fb48e6bb.zip | |
Merge pull request #11900 from simongreen-net/master
sgreen solutions to challenge 317
Diffstat (limited to 'challenge-317/sgreen/python')
| -rwxr-xr-x | challenge-317/sgreen/python/ch-1.py | 21 | ||||
| -rwxr-xr-x | challenge-317/sgreen/python/ch-2.py | 37 | ||||
| -rwxr-xr-x | challenge-317/sgreen/python/test.py | 24 |
3 files changed, 82 insertions, 0 deletions
diff --git a/challenge-317/sgreen/python/ch-1.py b/challenge-317/sgreen/python/ch-1.py new file mode 100755 index 0000000000..3300ed4a9d --- /dev/null +++ b/challenge-317/sgreen/python/ch-1.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +import sys + + +def is_acronyms(array: list, word: str) -> bool: + # Make up the acronym for the first letter in each word + acronym = ''.join(s[0] for s in array) + + # Compare it to the supplied string + return acronym.lower() == word.lower() + + +def main(): + # The last string is the word (acronym) + result = is_acronyms(sys.argv[1:-1], sys.argv[-1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-317/sgreen/python/ch-2.py b/challenge-317/sgreen/python/ch-2.py new file mode 100755 index 0000000000..b855287fdc --- /dev/null +++ b/challenge-317/sgreen/python/ch-2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +from collections import Counter +import sys + + +def friendly_strings(str1: str, str2: str) -> bool: + # Check strings are the same length + if len(str1) != len(str2): + return False + + if str1 == str2: + # We can still swap two characters if there are any letters that + # appear two or more times + freq = Counter(str1) + return any(i for i in freq.values() if i > 1) + + # Find characters that are different + differences = [ i for i in range(len(str1)) if str1[i] != str2[i]] + + if len(differences) == 2: + # Check that the letters at each position were switched + pos1, pos2 = differences + if str1[pos1] == str2[pos2] and str2[pos1] == str1[pos2]: + return True + + return False + + + +def main(): + result = friendly_strings(sys.argv[1], sys.argv[2]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-317/sgreen/python/test.py b/challenge-317/sgreen/python/test.py new file mode 100755 index 0000000000..7719735c43 --- /dev/null +++ b/challenge-317/sgreen/python/test.py @@ -0,0 +1,24 @@ +#!/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.assertTrue(ch_1.is_acronyms(["Perl", "Weekly", "Challenge"], "PWC")) + self.assertTrue(ch_1.is_acronyms(["Bob", "Charlie", "Joe"], "BCJ")) + self.assertFalse(ch_1.is_acronyms(["Morning", "Good"], "MM")) + + def test_ch_2(self): + self.assertTrue(ch_2.friendly_strings("desc", "dsec")) + self.assertTrue(ch_2.friendly_strings("fcyn", "fcny")) + self.assertFalse(ch_2.friendly_strings("poo", "eop")) + self.assertTrue(ch_2.friendly_strings("stripe", "sprite")) + self.assertTrue(ch_2.friendly_strings("bitter", "bitter")) + self.assertFalse(ch_2.friendly_strings("biter", "biter")) + + +if __name__ == '__main__': + unittest.main() |
