diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-18 23:23:44 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-18 23:23:44 +0100 |
| commit | 06aad1823095b3d094fdbe8eed699511be66482c (patch) | |
| tree | 4fc67febbbfafaff2070c4ca194a685853b6b37c | |
| parent | 36ff7b6646e15c3a76c287ce4d4462b1d251b101 (diff) | |
| parent | c9e4bbbcf9a747d9f2e268a0128b012a1215f57d (diff) | |
| download | perlweeklychallenge-club-06aad1823095b3d094fdbe8eed699511be66482c.tar.gz perlweeklychallenge-club-06aad1823095b3d094fdbe8eed699511be66482c.tar.bz2 perlweeklychallenge-club-06aad1823095b3d094fdbe8eed699511be66482c.zip | |
Merge pull request #12039 from oWnOIzRi/week321
add solution week 321 task 1 in python
| -rw-r--r-- | challenge-321/steven-wilson/python/ch-1.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/challenge-321/steven-wilson/python/ch-1.py b/challenge-321/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..315bde08b5 --- /dev/null +++ b/challenge-321/steven-wilson/python/ch-1.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +from numbers import Real + + +def distinct_average(numbers): + """ Given an array of numbers with even length, return the count of + distinct average. The average is calculate by removing the minimum and the + maximum, then average of the two. + + >>> distinct_average([1, 2, 4, 3, 5, 6]) + 1 + >>> distinct_average([0, 2, 4, 8, 3, 5]) + 2 + >>> distinct_average([7, 3, 1, 0, 5, 9]) + 2 + """ + if not all(isinstance(n, (Real)) and not isinstance(n, bool) for n in numbers): + raise TypeError("Array should comtain only numbers") + + if len(numbers) % 2 != 0: + raise ValueError("Array should be of even length") + + sorted_numbers = sorted(numbers) + split = int(len(sorted_numbers) / 2) + return len({(a + b) / 2 for a, b in + zip(sorted_numbers[:split], sorted_numbers[:split-1:-1])}) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) |
