aboutsummaryrefslogtreecommitdiff
path: root/challenge-324/packy-anderson/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-324/packy-anderson/python/ch-2.py')
-rwxr-xr-xchallenge-324/packy-anderson/python/ch-2.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/challenge-324/packy-anderson/python/ch-2.py b/challenge-324/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..3ef70423f5
--- /dev/null
+++ b/challenge-324/packy-anderson/python/ch-2.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+
+from functools import reduce
+
+def powerSet(ints):
+ return reduce(lambda r, i: r + [s + [i] for s in r], ints, [[]])
+
+def totalXOR(ints):
+ sum = 0
+ for s in powerSet(ints):
+ sum += reduce(lambda a, b: a ^ b, s, 0)
+ return sum
+
+def int_join(joiner, arr):
+ return joiner.join(map(lambda i: str(i), arr))
+
+def solution(ints):
+ print(f'Input: @ints = ({int_join(", ", ints)})')
+ print(f'Output: {totalXOR(ints)}')
+
+print('Example 1:')
+solution([1, 3])
+
+print('\nExample 2:')
+solution([5, 1, 6])
+
+print('\nExample 3:')
+solution([3, 4, 5, 6, 7, 8])