aboutsummaryrefslogtreecommitdiff
path: root/challenge-277/steven-wilson/python/ch-1.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-277/steven-wilson/python/ch-1.py')
-rw-r--r--challenge-277/steven-wilson/python/ch-1.py26
1 files changed, 26 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)