Program to find probability of getting assigned seat for the last person in an airplane after seat shuffling in Python

PythonServer Side ProgrammingProgramming

Suppose we have an integer n, that is representing the number of seats in an airplane. Now consider the first passenger has lost his ticket, so he picks a random seat. Now everyone else has their ticket but if their seat is already taken, they will also select an available seat randomly. We have 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, because either they have got the correct seat or not, so probability is always 50%, but for n = 1, it will be 100%

To solve this, we will follow these steps −

  • return "50%" if n > 1 otherwise "100%"

Example

Let us see the following implementation to get better understanding −

def solve(n):
   return "50%" if n > 1 else "100%"

n = 5
print(solve(n))

Input

5

Output

50%
raja
Published on 14-Oct-2021 12:34:31
Advertisements