Last active
August 29, 2015 14:27
-
-
Save altaurog/1b1d052e26c0af306dc4 to your computer and use it in GitHub Desktop.
Demonstrate solution to death row problem
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
import operator | |
import random | |
import sys | |
# https://en.wikipedia.org/wiki/Prisoners_and_hats_puzzle#Countably_Infinite_Hat_Problem_with_Hearing | |
def answer(death_row, answers): | |
return reduce(operator.xor, death_row + answers) | |
def main(death_row): | |
answers = [] | |
while death_row: | |
answers.append(answer(death_row[1:], answers)) | |
death_row.pop(0) | |
return answers | |
if __name__ == '__main__': | |
count = int(sys.argv[1]) | |
random.seed() | |
death_row = [random.choice([0,1]) for i in xrange(count)] | |
print death_row | |
print main(death_row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment