diff options
| author | Michael Manring <michael@manring> | 2024-07-08 14:30:08 +1000 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2024-07-08 14:34:20 +1000 |
| commit | 062951f4582d6d397b1a924a5277e81c369407af (patch) | |
| tree | af4ba083fc8aa39a7defa0579d30acef20805871 | |
| parent | 996112d2e3daa2be2425e08aec3ce172f71b841b (diff) | |
| download | perlweeklychallenge-club-062951f4582d6d397b1a924a5277e81c369407af.tar.gz perlweeklychallenge-club-062951f4582d6d397b1a924a5277e81c369407af.tar.bz2 perlweeklychallenge-club-062951f4582d6d397b1a924a5277e81c369407af.zip | |
pwc277 solution in python
| -rw-r--r-- | challenge-277/pokgopun/python/ch-1.py | 62 | ||||
| -rw-r--r-- | challenge-277/pokgopun/python/ch-2.py | 62 |
2 files changed, 124 insertions, 0 deletions
diff --git a/challenge-277/pokgopun/python/ch-1.py b/challenge-277/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..bd99b9d013 --- /dev/null +++ b/challenge-277/pokgopun/python/ch-1.py @@ -0,0 +1,62 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-277/ +""" + +Task 1: Count Common + +Submitted by: [53]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two array of strings, @words1 and @words2. + + Write a script to return the count of words that appears in both arrays + exactly once. + +Example 1 + +Input: @words1 = ("Perl", "is", "my", "friend") + @words2 = ("Perl", "and", "Raku", "are", "friend") +Output: 2 + +The words "Perl" and "friend" appear once in each array. + +Example 2 + +Input: @words1 = ("Perl", "and", "Python", "are", "very", "similar") + @words2 = ("Python", "is", "top", "in", "guest", "languages") +Output: 1 + +Example 3 + +Input: @words1 = ("Perl", "is", "imperative", "Lisp", "is", "functional") + @words2 = ("Crystal", "is", "similar", "to", "Ruby") +Output: 0 + +Task 2: Strong Pair +""" +### solution by pokgopun@gmail.com + +def countCommon(words1, words2): + return len( + set( + e for e in set(words1) if words1.count(e)==1 + ).intersection( + set( + e for e in set(words2) if words2.count(e)==1 + ) + ) + ) + +import unittest + +class TestCountCommon(unittest.TestCase): + def test(self): + for (words1, words2), cc in { + (("Perl", "is", "my", "friend"),("Perl", "and", "Raku", "are", "friend")): 2, + (("Perl", "and", "Python", "are", "very", "similar"),("Python", "is", "top", "in", "guest", "languages")): 1, + (("Perl", "is", "imperative", "Lisp", "is", "functional"),("Crystal", "is", "similar", "to", "Ruby")): 0, + }.items(): + self.assertEqual(countCommon(words1,words2),cc) + +unittest.main() + + diff --git a/challenge-277/pokgopun/python/ch-2.py b/challenge-277/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..2da0dfe4c3 --- /dev/null +++ b/challenge-277/pokgopun/python/ch-2.py @@ -0,0 +1,62 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-277/ +""" + +Task 2: Strong Pair + +Submitted by: [54]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to return the count of all strong pairs in the given + array. + + A pair of integers x and y is called strong pair if it satisfies: 0 + < |x - y| < min(x, y). + +Example 1 + +Input: @ints = (1, 2, 3, 4, 5) +Ouput: 4 + +Strong Pairs: (2, 3), (3, 4), (3, 5), (4, 5) + +Example 2 + +Input: @ints = (5, 7, 1, 7) +Ouput: 1 + +Strong Pairs: (5, 7) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 14th July 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def strongPair(ints): + ints = tuple(set(ints)) + c = 0 + l = len(ints) + for i in range(l-1): + for j in range(i+1,l): + x, y = ints[i], ints[j] + if x == y: + continue + if max(x,y) < 2*min(x,y): + c += 1 + return c + +import unittest + +class TestStrongPair(unittest.TestCase): + def test(self): + for ints, count in { + (1, 2, 3, 4, 5): 4, + (5, 7, 1, 7): 1, + }.items(): + self.assertEqual(strongPair(ints),count) + +unittest.main() |
