Page MenuHomePhabricator

drunk passenger

Authored by chris on Sep 24 2019, 7:20 PM.
import random
class Person:
def __init__(self, id: int) -> None: int = None int = id
self.is_drunk: bool = False
def create_passengers() -> list:
passengers = []
for passenger_id in range(100):
return passengers
def assign_seats(passengers: list, available_seats: list) -> bool:
for passenger in passengers[1:]:
if in available_seats: =
_seat = available_seats[
random.sample(range(len(available_seats)), 1)[0]] = _seat
return passengers[99].id == passengers[99].seat
if __name__ == '__main__':
_results = []
for i in range(1000):
available_seats = [x for x in range(0, 100)]
passengers = create_passengers()
passengers[0].is_drunk = True
passengers[0].seat = available_seats[random.sample(range(100), 1)[0]]
_results.append(assign_seats(passengers, available_seats))
print(sum(_results) / len(_results))

Event Timeline

chris created this paste.Sep 24 2019, 7:20 PM

gist is, you've got a plane with 100 passengers, each assigned a seat. The first passenger to board is drunk and chooses a random seat assignment. Each subsequent passenger to board will choose his or her own seat if it is available, or a random seat if not. What is the probability that the final passenger to board will remain in his or her assigned seat?