aboutsummaryrefslogtreecommitdiff
path: root/challenge-059/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2024-09-16 09:41:35 +0200
committerLubos Kolouch <lubos@kolouch.net>2024-09-16 09:41:35 +0200
commit65b9d6b25e0a823ca7ab6d15744ff98eb3697471 (patch)
treefcbcdebd50e3e146dfecf519701ec04b191053eb /challenge-059/paulo-custodio/python/ch-2.py
parentbd1fe7ae50ca42bda58c134b9edfdc287fb3f386 (diff)
parent68e321dd32a834f54b55d5e8924f04358e41cf1f (diff)
downloadperlweeklychallenge-club-65b9d6b25e0a823ca7ab6d15744ff98eb3697471.tar.gz
perlweeklychallenge-club-65b9d6b25e0a823ca7ab6d15744ff98eb3697471.tar.bz2
perlweeklychallenge-club-65b9d6b25e0a823ca7ab6d15744ff98eb3697471.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-059/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-059/paulo-custodio/python/ch-2.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/challenge-059/paulo-custodio/python/ch-2.py b/challenge-059/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..89d4f6741d
--- /dev/null
+++ b/challenge-059/paulo-custodio/python/ch-2.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python3
+
+# Challenge 059
+#
+# TASK #2 > Bit Sum
+# Reviewed by Ryan Thompson
+# Helper Function
+# For this task, you will most likely need a function f(a,b) which returns the
+# count of different bits of binary representation of a and b.
+#
+# For example, f(1,3) = 1, since:
+#
+# Binary representation of 1 = 01
+#
+# Binary representation of 3 = 11
+#
+# There is only 1 different bit. Therefore the subroutine should return 1. Note
+# that if one number is longer than the other in binary, the most significant
+# bits of the smaller number are padded (i.e., they are assumed to be zeroes).
+#
+# Script Output
+# You script should accept n positive numbers. Your script should sum the result
+# of f(a,b) for every pair of numbers given:
+#
+# For example, given 2, 3, 4, the output would be 6,
+# since f(2,3) + f(2,4) + f(3,4) = 1 + 2 + 3 = 6
+
+import sys
+from itertools import combinations
+
+def f(a, b):
+ r = int(a) ^ int(b)
+ rt = bin(r)
+ return rt.count('1')
+
+n = list(map(int, sys.argv[1:]))
+sum_ = 0
+
+for combin in combinations(n, 2):
+ sum_ += f(combin[0], combin[1])
+
+print(sum_)