Skip to content

Instantly share code, notes, and snippets.

@doodzik
Forked from anonymous/secretary_bromskloss.py
Created November 3, 2017 17:44
Show Gist options
  • Save doodzik/ea3009f0e781e5ec79622a70fba99b2e to your computer and use it in GitHub Desktop.
Save doodzik/ea3009f0e781e5ec79622a70fba99b2e to your computer and use it in GitHub Desktop.
Secretary (Bromskloss)
#!/usr/bin/env python3
# coding: utf-8
from math import e
from random import shuffle
from itertools import islice
def secretary_search(candidates):
"""Rejects first n / e candidates and then selects the first
better (or the last one, if there is no one better)."""
n_reject = int(len(candidates) / e)
last = candidates[-1]
candidates = iter(candidates)
best = max(islice(candidates, n_reject), default=-1)
return next(filter(lambda c: c > best, candidates), last)
n_candidates = 100
assert n_candidates >= 1, "At least one candidate, please."
n_trials = 10000
candidates = list(range(n_candidates))
hires = [0]*n_candidates
for _ in range(n_trials):
shuffle(candidates)
hires[secretary_search(candidates)] += 1
hires = [h/n_trials for h in hires]
# print(hires) # Probability distribution over candidates.
print(hires[-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment