
Untitled
By: a guest on
May 9th, 2012 | syntax:
None | size: 1.67 KB | hits: 14 | expires: Never
import random
def their_test(n):
# keep track of the average
total = 0
# do n tests
for i in range(n):
# special parameters
special_count = 0
hit_light = [False]*100
# global parameters
light_on = True
t = 0
# perform the test
while True:
t += 1
# pick a random prisoner [0-99]
p = random.randint(0, 99)
if p == 0:
# p is the 'special prisoner'
if light_on and t != 1:
light_on = False
special_count += 1
else:
# p is a normal prisoner
if not light_on and not hit_light[p]:
light_on = True
hit_light[p] = True
# termination condition
if special_count == 99:
break
# add to the total
total += t
# compute and print the average
print "average # of days:", (total/float(n))
def my_test(n):
# keep track of the average
total = 0
# do n tests
for i in range(n):
# global parameters
light_on = True
t = 0
# perform the test
while True:
# special parameters
hit_count = [0]*100
terminate = False
# divide time into 100-day blocks
for d in range(100):
t += 1
# pick a random prisoner [0-99]
p = random.randint(0, 99)
# check the day
if d == 99:
# it's the last day; terminate if the light is still on
if light_on:
terminate = True
break
light_on = True
else:
# if the prisoner has seen the room multiple times, he turns off the light
hit_count[p] += 1
if hit_count[p] == 2:
light_on = False
if terminate:
break
# add to the total
total += t
# compute and print the average
print "average # of days:", (total/float(n))
their_test(500)
my_test(1)