Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 1.67 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import random
  2.  
  3. def their_test(n):
  4.         # keep track of the average
  5.         total = 0
  6.         # do n tests
  7.         for i in range(n):
  8.                 # special parameters
  9.                 special_count = 0
  10.                 hit_light = [False]*100
  11.                 # global parameters
  12.                 light_on = True
  13.                 t = 0
  14.                 # perform the test
  15.                 while True:
  16.                         t += 1
  17.                         # pick a random prisoner [0-99]
  18.                         p = random.randint(0, 99)
  19.                         if p == 0:
  20.                                 # p is the 'special prisoner'
  21.                                 if light_on and t != 1:
  22.                                         light_on = False
  23.                                         special_count += 1
  24.                         else:
  25.                                 # p is a normal prisoner
  26.                                 if not light_on and not hit_light[p]:
  27.                                         light_on = True
  28.                                         hit_light[p] = True
  29.                         # termination condition
  30.                         if special_count == 99:
  31.                                 break
  32.                 # add to the total
  33.                 total += t
  34.         # compute and print the average
  35.         print "average # of days:", (total/float(n))
  36.  
  37. def my_test(n):
  38.         # keep track of the average
  39.         total = 0
  40.         # do n tests
  41.         for i in range(n):
  42.                 # global parameters
  43.                 light_on = True
  44.                 t = 0
  45.                 # perform the test
  46.                 while True:
  47.                         # special parameters
  48.                         hit_count = [0]*100
  49.                         terminate = False
  50.                         # divide time into 100-day blocks
  51.                         for d in range(100):
  52.                                 t += 1
  53.                                 # pick a random prisoner [0-99]
  54.                                 p = random.randint(0, 99)
  55.                                 # check the day
  56.                                 if d == 99:
  57.                                         # it's the last day; terminate if the light is still on
  58.                                         if light_on:
  59.                                                 terminate = True
  60.                                                 break
  61.                                         light_on = True
  62.                                 else:
  63.                                         # if the prisoner has seen the room multiple times, he turns off the light
  64.                                         hit_count[p] += 1
  65.                                         if hit_count[p] == 2:
  66.                                                 light_on = False
  67.                         if terminate:
  68.                                 break
  69.                 # add to the total
  70.                 total += t
  71.         # compute and print the average
  72.         print "average # of days:", (total/float(n))
  73.  
  74. their_test(500)
  75. my_test(1)