diff options
| author | Steven <steven1170@zoho.eu> | 2025-05-18 14:52:29 +0100 |
|---|---|---|
| committer | Steven <steven1170@zoho.eu> | 2025-05-18 14:52:29 +0100 |
| commit | c9e4bbbcf9a747d9f2e268a0128b012a1215f57d (patch) | |
| tree | b6ca853580751f59ad4ffcd0d502a39ea04cfd5d | |
| parent | af05fe109d571105d3c3152ff411795b40398ecb (diff) | |
| download | perlweeklychallenge-club-c9e4bbbcf9a747d9f2e268a0128b012a1215f57d.tar.gz perlweeklychallenge-club-c9e4bbbcf9a747d9f2e268a0128b012a1215f57d.tar.bz2 perlweeklychallenge-club-c9e4bbbcf9a747d9f2e268a0128b012a1215f57d.zip | |
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) |
