diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-09 17:31:00 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-09 17:31:00 +0100 |
| commit | 236e171d907c2b05f699de95aa43cebdad89d03a (patch) | |
| tree | 0fbd611a32674d6d7c6041bc606d2a603b7b5c40 | |
| parent | 69e1b4917a039e62ed0ebc59cb4b00c74d71a9a5 (diff) | |
| parent | f3de56b91c59890c6e20d8c8bfcea04199528930 (diff) | |
| download | perlweeklychallenge-club-236e171d907c2b05f699de95aa43cebdad89d03a.tar.gz perlweeklychallenge-club-236e171d907c2b05f699de95aa43cebdad89d03a.tar.bz2 perlweeklychallenge-club-236e171d907c2b05f699de95aa43cebdad89d03a.zip | |
Merge pull request #10402 from oWnOIzRi/week277
add solutions week 277 in python
| -rw-r--r-- | challenge-277/steven-wilson/python/ch-1.py | 26 | ||||
| -rw-r--r-- | challenge-277/steven-wilson/python/ch-2.py | 25 |
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) |
