aboutsummaryrefslogtreecommitdiff
path: root/challenge-308/steven-wilson/python/ch-2.py
diff options
context:
space:
mode:
authorSteven <steven1170@zoho.eu>2025-02-11 16:31:46 +0000
committerSteven <steven1170@zoho.eu>2025-02-11 16:31:46 +0000
commit4e45c44c570fd4a77a5a76236bb763d4d9b859ec (patch)
tree5b2a0e575b148d21c3512a6344e0afe933652301 /challenge-308/steven-wilson/python/ch-2.py
parentd8179c22c12d35d4201bc8e3f759a4a8009e6b1b (diff)
downloadperlweeklychallenge-club-4e45c44c570fd4a77a5a76236bb763d4d9b859ec.tar.gz
perlweeklychallenge-club-4e45c44c570fd4a77a5a76236bb763d4d9b859ec.tar.bz2
perlweeklychallenge-club-4e45c44c570fd4a77a5a76236bb763d4d9b859ec.zip
add solutions week 308 in python & perl
Diffstat (limited to 'challenge-308/steven-wilson/python/ch-2.py')
-rw-r--r--challenge-308/steven-wilson/python/ch-2.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/challenge-308/steven-wilson/python/ch-2.py b/challenge-308/steven-wilson/python/ch-2.py
new file mode 100644
index 0000000000..285f6dd2d7
--- /dev/null
+++ b/challenge-308/steven-wilson/python/ch-2.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python3
+
+
+def decode_xor(initial, encoded):
+ """ Given an encoded array and an initial integer, find the original array
+ that produced the given encoded array. It was encoded such that
+ encoded[i] = orig[i] XOR orig[i + 1].
+
+ >>> decode_xor(1, (1, 2, 3))
+ (1, 0, 2, 1)
+ >>> decode_xor(4, (6, 2, 7, 3))
+ (4, 2, 0, 7, 4)
+ """
+ decoded = [initial]
+
+ for e in encoded:
+ decoded.append(decoded[-1] ^ e)
+
+ return tuple(decoded)
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)