Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find probability of getting assigned seat for the last person in an airplane after seat shuffling in Python
Suppose we have an integer n, representing the number of seats in an airplane. The first passenger has lost his ticket, so he picks a random seat. Everyone else has their ticket, but if their seat is already taken, they will also select an available seat randomly. We need to find the probability that the last person gets their assigned seat.
So, if the input is like n = 5, then the output will be 0.5. The answer is always constant when there is more than one person - either they get the correct seat or not, so probability is always 50%. For n = 1, it will be 100%.
Mathematical Analysis
This problem has an elegant mathematical solution ?
- If n = 1: The only passenger sits in their own seat (100% probability)
- If n > 1: The first passenger either picks seat 1 (his own) or seat n (last person's seat), or any middle seat
- If he picks seat 1: Everyone sits correctly (last person gets their seat)
- If he picks seat n: Last person cannot get their seat
- If he picks middle seat k: Person k will face the same choice as the first passenger
- This reduces to a 50-50 probability between seats 1 and n
Solution Implementation
def solve(n):
return "50%" if n > 1 else "100%"
# Test with different values
test_cases = [1, 2, 5, 10, 100]
for n in test_cases:
probability = solve(n)
print(f"For n = {n}: Probability = {probability}")
For n = 1: Probability = 100% For n = 2: Probability = 50% For n = 5: Probability = 50% For n = 10: Probability = 50% For n = 100: Probability = 50%
Alternative Implementation with Decimal Values
We can also return decimal probability values ?
def solve_decimal(n):
return 1.0 if n == 1 else 0.5
# Test the decimal version
test_cases = [1, 3, 7, 50]
for n in test_cases:
probability = solve_decimal(n)
print(f"Seats: {n}, Probability: {probability}")
Seats: 1, Probability: 1.0 Seats: 3, Probability: 0.5 Seats: 7, Probability: 0.5 Seats: 50, Probability: 0.5
Step-by-Step Example
Let's trace through a small example with n = 3 ?
def explain_example():
print("Airplane with 3 seats: [1, 2, 3]")
print("Passengers assigned to: [1, 2, 3]")
print("\nFirst passenger (lost ticket) can choose:")
print("- Seat 1 (his own): Everyone sits correctly ? Last person gets seat 3 ?")
print("- Seat 2: Passenger 2 faces same choice (seat 1 or 3)")
print("- Seat 3 (last person's): Last person cannot get their seat ?")
print("\nResult: 50% probability the last person gets their assigned seat")
explain_example()
Airplane with 3 seats: [1, 2, 3] Passengers assigned to: [1, 2, 3] First passenger (lost ticket) can choose: - Seat 1 (his own): Everyone sits correctly ? Last person gets seat 3 ? - Seat 2: Passenger 2 faces same choice (seat 1 or 3) - Seat 3 (last person's): Last person cannot get their seat ? Result: 50% probability the last person gets their assigned seat
Conclusion
This airplane seating problem demonstrates that for any n > 1, the probability is exactly 50%. The recursive nature of seat selection always reduces to a binary choice between the first and last seats, making this an elegant mathematical puzzle.
