diff options
| author | 冯昶 <fengchang@novel-supertv.com> | 2024-03-04 16:39:31 +0800 |
|---|---|---|
| committer | 冯昶 <fengchang@novel-supertv.com> | 2024-03-04 16:39:31 +0800 |
| commit | 8edd4508d3af07ed272eec6ceeca1a9789b5e68a (patch) | |
| tree | 2db742aea75a7a3005575c04a7e44da6d5c88a1f /challenge-258/packy-anderson/python | |
| parent | d64db60d9dcf17d59060a03f5b1fbc5e82fc1953 (diff) | |
| parent | ad381c1123e47114d3d98f1749f9f354eebafcdd (diff) | |
| download | perlweeklychallenge-club-8edd4508d3af07ed272eec6ceeca1a9789b5e68a.tar.gz perlweeklychallenge-club-8edd4508d3af07ed272eec6ceeca1a9789b5e68a.tar.bz2 perlweeklychallenge-club-8edd4508d3af07ed272eec6ceeca1a9789b5e68a.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-258/packy-anderson/python')
| -rwxr-xr-x | challenge-258/packy-anderson/python/ch-1.py | 25 | ||||
| -rwxr-xr-x | challenge-258/packy-anderson/python/ch-2.py | 34 |
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-258/packy-anderson/python/ch-1.py b/challenge-258/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..fb5188ace1 --- /dev/null +++ b/challenge-258/packy-anderson/python/ch-1.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python + +from math import floor, log10 + +def evenDigitCount(ints): + count = 0; # in case there are no even digit ints + for n in ints: + if floor(log10(n) + 1) % 2 == 0: count += 1 + return count + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @arr = ({comma_join(ints)})') + print(f'Output: {evenDigitCount(ints)}') + +print('Example 1:') +solution([10, 1, 111, 24, 1000]) + +print('\nExample 2:') +solution([111, 1, 11111]) + +print('\nExample 3:') +solution([2, 8, 1024, 256]) diff --git a/challenge-258/packy-anderson/python/ch-2.py b/challenge-258/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..b9bdce3bb0 --- /dev/null +++ b/challenge-258/packy-anderson/python/ch-2.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +from functools import cache + +@cache +def setBitCount(i): + count = 0 + bit = 1 + while (bit <= i): + if i & bit: count += 1 # count if we have this bit set + bit <<= 1 # shift bits left, ie 10 becomes 100 + return count + +def valueSum(k, ints): + sum = 0 + for i in range(len(ints)): + if setBitCount(i) == k: sum += ints[i] + return sum + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(k, ints): + print(f'Input: @ints = ({comma_join(ints)}), $k = {k}') + print(f'Output: {valueSum(k, ints)}') + +print('Example 1:') +solution(1, [2, 5, 9, 11, 3]) + +print('\nExample 2:') +solution(2, [2, 5, 9, 11, 3]) + +print('\nExample 3:') +solution(0, [2, 5, 9, 11, 3]) |
