diff options
Diffstat (limited to 'challenge-277/steven-wilson/python/ch-2.py')
| -rw-r--r-- | challenge-277/steven-wilson/python/ch-2.py | 25 |
1 files changed, 25 insertions, 0 deletions
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) |
