diff options
| author | Simon Green <mail@simon.green> | 2024-03-03 22:06:55 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2024-03-03 22:06:55 +1100 |
| commit | d421c52a28a04293c879ba2fd05e66ab8ee840c7 (patch) | |
| tree | 87b8cb291b2889a6b27f50759937bc5a06432888 /challenge-258/sgreen/python/ch-2.py | |
| parent | 4416b8cd33659c6d380e3ea2c5b3e21e4a861a99 (diff) | |
| download | perlweeklychallenge-club-d421c52a28a04293c879ba2fd05e66ab8ee840c7.tar.gz perlweeklychallenge-club-d421c52a28a04293c879ba2fd05e66ab8ee840c7.tar.bz2 perlweeklychallenge-club-d421c52a28a04293c879ba2fd05e66ab8ee840c7.zip | |
Simon's solution to challenge 258
Diffstat (limited to 'challenge-258/sgreen/python/ch-2.py')
| -rwxr-xr-x | challenge-258/sgreen/python/ch-2.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/challenge-258/sgreen/python/ch-2.py b/challenge-258/sgreen/python/ch-2.py new file mode 100755 index 0000000000..f670d54629 --- /dev/null +++ b/challenge-258/sgreen/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import sys + + +def sum_of_values(ints: list, k: int) -> int: + total = 0 + + for pos, value in enumerate(ints): + # Count the number of ones in the position value + if k == bin(pos).count('1'): + # If it matches the 'k', add the value to the total + total += value + + return total + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + k = array.pop() + result = sum_of_values(array, k) + print(result) + + +if __name__ == '__main__': + main() |
