Skip to content

Instantly share code, notes, and snippets.

@altaurog
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save altaurog/1b1d052e26c0af306dc4 to your computer and use it in GitHub Desktop.
Save altaurog/1b1d052e26c0af306dc4 to your computer and use it in GitHub Desktop.
Demonstrate solution to death row problem
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