Page MenuHomePhabricator

drunk passenger
ActivePublic

Authored by chris on Sep 24 2019, 7:20 PM.
import random
class Person:
def __init__(self, id: int) -> None:
self.seat: int = None
self.id: int = id
self.is_drunk: bool = False
def create_passengers() -> list:
passengers = []
for passenger_id in range(100):
passengers.append(Person(passenger_id))
return passengers
def assign_seats(passengers: list, available_seats: list) -> bool:
for passenger in passengers[1:]:
if passenger.id in available_seats:
passenger.seat = passenger.id
available_seats.pop(
available_seats.index(passenger.seat))
else:
_seat = available_seats[
random.sample(range(len(available_seats)), 1)[0]]
passenger.seat = _seat
available_seats.pop(available_seats.index(passenger.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]]
available_seats.pop(passengers[0].seat)
_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?