diff options
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() |
