Created
October 22, 2020 01:32
-
-
Save duanebester/9ee93400ce988379640f1ece04d6fbe9 to your computer and use it in GitHub Desktop.
Python Workers Checksum Challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Faster XOR | |
def getXor(a, b): | |
return [b, 1, b + 1, 0][b % 4] ^ [(a-1), 1, (a-1) + 1, 0][(a-1) % 4] | |
def solution(start, length): | |
l = length | |
cs = 0 | |
while l > 0: | |
cs ^= getXor(start, start + l - 1) | |
start = start + length | |
l = l - 1 | |
return cs | |
print solution(0, 3) # 2 | |
print solution(17, 4) # 14 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment