diff options
| author | Michael Manring <michael@manring> | 2024-02-26 18:50:34 +1100 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2024-02-27 01:34:57 +1100 |
| commit | 659cdb7fad452141e1b2ec4a2bb14a9765501a6d (patch) | |
| tree | 6a20cff95c665cabae05226c97f7df8955ab5756 | |
| parent | 4416b8cd33659c6d380e3ea2c5b3e21e4a861a99 (diff) | |
| download | perlweeklychallenge-club-659cdb7fad452141e1b2ec4a2bb14a9765501a6d.tar.gz perlweeklychallenge-club-659cdb7fad452141e1b2ec4a2bb14a9765501a6d.tar.bz2 perlweeklychallenge-club-659cdb7fad452141e1b2ec4a2bb14a9765501a6d.zip | |
pwc258 solution in python
| -rw-r--r-- | challenge-258/pokgopun/python/ch-1.py | 51 | ||||
| -rw-r--r-- | challenge-258/pokgopun/python/ch-2.py | 64 |
2 files changed, 115 insertions, 0 deletions
diff --git a/challenge-258/pokgopun/python/ch-1.py b/challenge-258/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..8232fe40f8 --- /dev/null +++ b/challenge-258/pokgopun/python/ch-1.py @@ -0,0 +1,51 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-258/ +""" + +Task 1: Count Even Digits Number + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a array of positive integers, @ints. + + Write a script to find out how many integers have even number of + digits. + +Example 1 + +Input: @ints = (10, 1, 111, 24, 1000) +Output: 3 + +There are 3 integers having even digits i.e. 10, 24 and 1000. + +Example 2 + +Input: @ints = (111, 1, 11111) +Output: 0 + +Example 3 + +Input: @ints = (2, 8, 1024, 256) +Output: 1 + +Task 2: Sum of Values +""" +### solution by pokgopun@gmail.com + +def CEDN(ints: tuple): + return sum( + len(str(e)) % 2 == 0 for e in ints + ) + +import unittest + +class TestCEDN(unittest.TestCase): + def test(self): + for inpt, otpt in { + (10, 1, 111, 24, 1000): 3, + (111, 1, 11111): 0, + (2, 8, 1024, 256): 1, + }.items(): + self.assertEqual(CEDN(inpt),otpt) + +unittest.main() diff --git a/challenge-258/pokgopun/python/ch-2.py b/challenge-258/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..049d7348b1 --- /dev/null +++ b/challenge-258/pokgopun/python/ch-2.py @@ -0,0 +1,64 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-258/ +""" + +Task 2: Sum of Values + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @int and an integer $k. + + Write a script to find the sum of values whose index binary + representation has exactly $k number of 1-bit set. + +Example 1 + +Input: @ints = (2, 5, 9, 11, 3), $k = 1 +Output: 17 + +Binary representation of index 0 = 0 +Binary representation of index 1 = 1 +Binary representation of index 2 = 10 +Binary representation of index 3 = 11 +Binary representation of index 4 = 100 + +So the indices 1, 2 and 4 have total one 1-bit sets. +Therefore the sum, $ints[1] + $ints[2] + $ints[3] = 17 + +Example 2 + +Input: @ints = (2, 5, 9, 11, 3), $k = 2 +Output: 11 + +Example 3 + +Input: @ints = (2, 5, 9, 11, 3), $k = 0 +Output: 2 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 1st March 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def SOV(ints: tuple, k: int): + return sum( + ints[i] for i in range(len(ints)) + if sum( + int(e) for e in bin(i).lstrip('0b') + ) == k + ) +import unittest + +class TestSOV(unittest.TestCase): + def test(self): + for (ints, k), otpt in { + ((2, 5, 9, 11, 3), 1): 17, + ((2, 5, 9, 11, 3), 2): 11, + ((2, 5, 9, 11, 3), 0): 2, + }.items(): + self.assertEqual(SOV(ints,k),otpt) + +unittest.main() |
