Skip to content

Instantly share code, notes, and snippets.

@bjornedstrom
Created August 29, 2016 17:10
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 bjornedstrom/971574557b6f3179db083508c8cf8c3b to your computer and use it in GitHub Desktop.
Save bjornedstrom/971574557b6f3179db083508c8cf8c3b to your computer and use it in GitHub Desktop.
"""
The names of 100 prisoners are placed in 100 wooden boxes, one name to a box, and the boxes are
lined up on a table in a room. One by one, the prisoners are led into the room; each may look
in at most 50 boxes, but must leave the room exactly as he found it and is permitted no further
communication with the others.
The prisoners have a chance to plot their strategy in advance, and they are going to need it,
because unless every single prisoner finds his own name all will subsequently be executed.
Find a strategy for them which which has probability of success exceeding 30%.
"""
import random
def game():
labeling = range(100)
random.shuffle(labeling)
for prisoner in range(100):
next = prisoner
found = False
for n in range(50):
cur = labeling.index(next)
if cur == prisoner:
found = True
next = cur
if not found:
return False
return True
TRIES = 1000
counts = {True: 0, False: 0}
for i in range(TRIES):
counts[game()] += 1
print counts[True] / float(TRIES)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment