aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-27 18:29:16 +0000
committerGitHub <noreply@github.com>2024-02-27 18:29:16 +0000
commit4dfcb6cf3f0a203258e88ce9b0f9e7c2ee6094a4 (patch)
treee334f5d09830c79c813f3f53e1800c3382c9b599
parent80c315229459ca1b2dd66e815d406c17770347b0 (diff)
parent2a833741d00379bb50c7574c305ec33a34357b31 (diff)
downloadperlweeklychallenge-club-4dfcb6cf3f0a203258e88ce9b0f9e7c2ee6094a4.tar.gz
perlweeklychallenge-club-4dfcb6cf3f0a203258e88ce9b0f9e7c2ee6094a4.tar.bz2
perlweeklychallenge-club-4dfcb6cf3f0a203258e88ce9b0f9e7c2ee6094a4.zip
Merge pull request #9666 from oWnOIzRi/week258
add solutions week 258 in python
-rw-r--r--challenge-258/steven-wilson/python/ch-1.py23
-rw-r--r--challenge-258/steven-wilson/python/ch-2.py51
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-258/steven-wilson/python/ch-1.py b/challenge-258/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..ae1ed7e216
--- /dev/null
+++ b/challenge-258/steven-wilson/python/ch-1.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python3
+
+
+def count_even_digits_number(*integers):
+ ''' Given a array of positive integers, find out how many have even
+ number of digits
+ >>> count_even_digits_number(10, 1, 111, 24, 1000)
+ 3
+ >>> count_even_digits_number(111, 1, 11111)
+ 0
+ >>> count_even_digits_number(2, 8, 1024, 256)
+ 1
+ '''
+ if not all(isinstance(num, int) and num > 0 for num in integers):
+ raise ValueError("Input must consist of positive integers")
+
+ return len([num for num in integers if len(str(num)) % 2 == 0])
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod()
diff --git a/challenge-258/steven-wilson/python/ch-2.py b/challenge-258/steven-wilson/python/ch-2.py
new file mode 100644
index 0000000000..f386802d75
--- /dev/null
+++ b/challenge-258/steven-wilson/python/ch-2.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+
+
+class SetBitSumCalculator:
+ def __init__(self):
+ self.count_indices = dict()
+ self.max_index = 0
+
+ def _count_one_bit_set(self, binary):
+ ''' Count the number of 1-bit sets'''
+ count = 0
+ for b in binary:
+ if b == "1":
+ count += 1
+ return count
+
+ def _populate_count_indices(self, list_length):
+ if list_length > self.max_index:
+ new_indices = range(self.max_index, list_length)
+ binaries = [bin(i) for i in new_indices]
+ one_bit = [self._count_one_bit_set(b) for b in binaries]
+ for num, count in enumerate(one_bit, start=self.max_index):
+ if str(count) in self.count_indices:
+ self.count_indices[str(count)].append(num)
+ else:
+ self.count_indices[str(count)] = [num]
+ self.max_index = list_length
+
+ def sum_of_values(self, *integers, k=1):
+ ''' Given an array of integers and an integer k, find the sum of values
+ whose index binary representation has exactly k number of 1-bit set.
+ >>> calculator = SetBitSumCalculator()
+ >>> calculator.sum_of_values(2, 5, 9, 11, 3, k=1)
+ 17
+ >>> calculator.sum_of_values(2, 5, 9, 11, 3, k=2)
+ 11
+ >>> calculator.sum_of_values(2, 5, 9, 11, 3, k=0)
+ 2
+ '''
+ if not isinstance(k, int):
+ raise TypeError("k is not a valid input")
+
+ len_integers = len(integers)
+ self._populate_count_indices(len_integers)
+ return sum(integers[x] for x in self.count_indices[str(k)] if x < len_integers)
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod()