Skip to content

Instantly share code, notes, and snippets.

@jchysk
Created May 27, 2014 03:09
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 jchysk/7582c7618f9a35b9b5bd to your computer and use it in GitHub Desktop.
Save jchysk/7582c7618f9a35b9b5bd to your computer and use it in GitHub Desktop.
Secretary problem; Daut version towards optimizing average instead
#Daut's edited secretary problem
#function which randomizes a list
def randomize_rankings(sec):
import random
random.shuffle(sec)
return sec
#function which returns a secretary based on Daut's algorithm
def chooseSecretary(sec):
max=sec[0]
max2=0
max3=0
max4=0
max5=0
max6=0
max7=0
max8=0
max9=0
max10=0
x=1
while x<36:
if (sec[x] > max):
max = sec[x]
x=x+1
while x<60:
if (sec[x] > max):
return sec[x]
if (sec[x] > max2):
max2 = sec[x]
x=x+1
while x<74:
if (sec[x] > max2):
return sec[x]
if (sec[x] > max3):
max3 = sec[x]
x=x+1
while x<84:
if (sec[x] > max3):
return sec[x]
if (sec[x] > max4):
max4 = sec[x]
x=x+1
while x<90:
if (sec[x] > max4):
return sec[x]
if (sec[x] > max5):
max5 = sec[x]
x=x+1
while x<93:
if (sec[x] > max5):
return sec[x]
if (sec[x] > max6):
max6 = sec[x]
x=x+1
while x<95:
if (sec[x] > max6):
return sec[x]
if (sec[x] > max7):
max7 = sec[x]
x=x+1
while x<97:
if (sec[x] > max7):
return sec[x]
if (sec[x] > max8):
max8 = sec[x]
x=x+1
while x<98:
if (sec[x] > max8):
return sec[x]
if (sec[x] > max9):
max9 = sec[x]
x=x+1
while x<99:
if (sec[x] > max9):
return sec[x]
if (sec[x] > max10):
max10 = sec[x]
x=x+1
return sec[99]
APPLICANTS = 100
TRIALS = 10000
sec = list(range(0, APPLICANTS))
b=0
bestCount=0
total=0
winners = []
for b in range(TRIALS):
secSorted = randomize_rankings(sec)
chosen = chooseSecretary(secSorted)
if(chosen == APPLICANTS - 1):
bestCount += 1
total += chosen
b += 1
winners.append(chosen)
print "Best count: %s" % bestCount
print "Picked the best: %s" % (bestCount / float(TRIALS))
print "Average: %s" % (total / TRIALS)
print "Median: %s" % sorted(winners)[len(winners)//2]
print "Bottom 10%% %s" % (sum(i < int(APPLICANTS / 10) for i in winners) / float(TRIALS))
#print "All Winners: %s" % sorted(winners)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment