aboutsummaryrefslogtreecommitdiff
path: root/challenge-258/sgreen/python/ch-2.py
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2024-03-03 22:06:55 +1100
committerSimon Green <mail@simon.green>2024-03-03 22:06:55 +1100
commitd421c52a28a04293c879ba2fd05e66ab8ee840c7 (patch)
tree87b8cb291b2889a6b27f50759937bc5a06432888 /challenge-258/sgreen/python/ch-2.py
parent4416b8cd33659c6d380e3ea2c5b3e21e4a861a99 (diff)
downloadperlweeklychallenge-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-xchallenge-258/sgreen/python/ch-2.py27
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()