diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-15 02:03:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-15 02:03:57 +0100 |
| commit | dbd7bb3c6cadb0ea57e80bf915516b0682cdef9c (patch) | |
| tree | 19666a69cc92c0b1da21b8c4d5b9f24391975489 /challenge-321/pokgopun/python/ch-1.py | |
| parent | 152c0ffaaa3df8dfa2a5dbff97971ec378999b0a (diff) | |
| parent | 7c3ee687c1e483f37869f932aebd08a0472ac7bb (diff) | |
| download | perlweeklychallenge-club-dbd7bb3c6cadb0ea57e80bf915516b0682cdef9c.tar.gz perlweeklychallenge-club-dbd7bb3c6cadb0ea57e80bf915516b0682cdef9c.tar.bz2 perlweeklychallenge-club-dbd7bb3c6cadb0ea57e80bf915516b0682cdef9c.zip | |
Merge pull request #12025 from pokgopun/pwc321
Pwc321
Diffstat (limited to 'challenge-321/pokgopun/python/ch-1.py')
| -rw-r--r-- | challenge-321/pokgopun/python/ch-1.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/challenge-321/pokgopun/python/ch-1.py b/challenge-321/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..d5ccc2fe7d --- /dev/null +++ b/challenge-321/pokgopun/python/ch-1.py @@ -0,0 +1,71 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-321/ +""" + +Task 1: Distinct Average + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of numbers with even length. + + Write a script to return the count of distinct average. The average is + calculate by removing the minimum and the maximum, then average of the + two. + +Example 1 + +Input: @nums = (1, 2, 4, 3, 5, 6) +Output: 1 + +Step 1: Min = 1, Max = 6, Avg = 3.5 +Step 2: Min = 2, Max = 5, Avg = 3.5 +Step 3: Min = 3, Max = 4, Avg = 3.5 + +The count of distinct average is 1. + +Example 2 + +Input: @nums = (0, 2, 4, 8, 3, 5) +Output: 2 + +Step 1: Min = 0, Max = 8, Avg = 4 +Step 2: Min = 2, Max = 5, Avg = 3.5 +Step 3: Min = 3, Max = 4, Avg = 3.5 + +The count of distinct average is 2. + +Example 3 + +Input: @nums = (7, 3, 1, 0, 5, 9) +Output: 2 + +Step 1: Min = 0, Max = 9, Avg = 4.5 +Step 2: Min = 1, Max = 7, Avg = 4 +Step 3: Min = 3, Max = 5, Avg = 4 + +The count of distinct average is 2. + +Task 2: Backspace Compare +""" +### solution by pokgopun@gmail.com + +def da(ints: tuple[int]) -> int: + l = len(ints) + lst = sorted(ints) + avgs = [] + for i in range(int(l/2)): + avgs.append((lst[i]+lst[l-1-i])/2) + return len(set(avgs)) + +import unittest + +class TestDa(unittest.TestCase): + def test(self): + for inpt, otpt in { + (1, 2, 4, 3, 5, 6): 1, + (0, 2, 4, 8, 3, 5): 2, + (7, 3, 1, 0, 5, 9): 2, + }.items(): + self.assertEqual(da(inpt), otpt) + +unittest.main() |
