aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven <steven1170@zoho.eu>2024-07-09 15:54:34 +0100
committerSteven <steven1170@zoho.eu>2024-07-09 15:54:34 +0100
commitf3de56b91c59890c6e20d8c8bfcea04199528930 (patch)
tree4e84d505b65ce9de840a2549b1158cfe324be3f6
parentbdbb62a1c5dabd33dcd16be7cd995ad7d73847d1 (diff)
downloadperlweeklychallenge-club-f3de56b91c59890c6e20d8c8bfcea04199528930.tar.gz
perlweeklychallenge-club-f3de56b91c59890c6e20d8c8bfcea04199528930.tar.bz2
perlweeklychallenge-club-f3de56b91c59890c6e20d8c8bfcea04199528930.zip
add solutions week 277 in python
-rw-r--r--challenge-277/steven-wilson/python/ch-1.py26
-rw-r--r--challenge-277/steven-wilson/python/ch-2.py25
2 files changed, 51 insertions, 0 deletions
diff --git a/challenge-277/steven-wilson/python/ch-1.py b/challenge-277/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..ce88071a6d
--- /dev/null
+++ b/challenge-277/steven-wilson/python/ch-1.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+from collections import Counter
+
+
+def count_common(words1, words2):
+ """ Given two array of strings, return the count of words that appears in
+ both arrays exactly once.
+
+ >>> count_common(("Perl", "is", "my", "friend"), ("Perl", "and", "Raku", "are", "friend"))
+ 2
+ >>> count_common(("Perl", "and", "Python", "are", "very", "similar"), ("Python", "is", "top", "in", "guest", "languages"))
+ 1
+ >>> count_common(("Perl", "is", "imperative", "Lisp", "is", "functional"), ("Crystal", "is", "similar", "to", "Ruby"))
+ 0
+ """
+ c1 = Counter(words1)
+ c2 = Counter(words2)
+ return len({k for k, v in c1.items() if v == 1} &
+ {k for k, v in c2.items() if v == 1})
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)
diff --git a/challenge-277/steven-wilson/python/ch-2.py b/challenge-277/steven-wilson/python/ch-2.py
new file mode 100644
index 0000000000..c75bd2bbb0
--- /dev/null
+++ b/challenge-277/steven-wilson/python/ch-2.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python3
+
+from itertools import combinations
+
+
+def strong_pair(*integers):
+ """ Given an array of integers, 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).
+
+ >>> strong_pair(1, 2, 3, 4, 5)
+ 4
+ >>> strong_pair(5, 7, 1, 7)
+ 1
+ """
+ return len({(x, y) for x, y in combinations(integers, 2)
+ if 0 < abs(x - y) and abs(x - y) < min(x, y)})
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)