Created
July 26, 2017 23:27
-
-
Save Noxville/f9c9add53779fa5c058abb1e0ff32ba5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import random | |
import csv | |
ITERATIONS = 10**5 | |
NUM_SEATS = 100 | |
_seats = {} | |
for i in xrange(NUM_SEATS): | |
_seats[i] = True | |
good = 0 | |
for _ in xrange(ITERATIONS): | |
seats = _seats.copy() | |
for passenger in xrange(NUM_SEATS): | |
if passenger == 0: | |
del seats[random.choice(list(seats))] # Random choice for first passenger | |
else: | |
if passenger in seats: | |
if passenger == NUM_SEATS - 1: # Does the last person get their seat? | |
good += 1 | |
del seats[passenger] # Mark passenger's seat as taken | |
else: | |
del seats[random.choice(list(seats))] # Mark random seat as taken | |
print "{} iterations, of which {} saw the last passenger get his seat".format(ITERATIONS, good) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment