Train Rotation Reachability Probability
Problem statement
A train has carCount distinct cars and one fixed target order. Its only allowed move removes the last car and puts that car at the front. The conductor may make zero or more moves.
For this exercise, assume the initial order is chosen uniformly from all permutations, and generalize the reported three-car question to the supplied carCount. Return the probability that the conductor can reach the target order.
For this exercise, assume a numeric result with relative-or-absolute tolerance 1e-9. Complete trainRotationProbability(carCount).
Function
trainRotationProbability(carCount: int) → doubleExamples
Example 1
carCount = 3return = 0.5For the reported three distinct cars, exactly three of the six initial orders are cyclic rotations of the target. The result is 3/6 = 0.5 under the stated uniform model.
Example 2
carCount = 1return = 1.0There is only one initial order; zero moves already reaches the target.
Example 3
carCount = 4return = 0.16666666666666666Exactly four of the 4! = 24 orders are reachable rotations of the target. The probability is 1/6.
Constraints
- For this exercise, assume
1 <= carCount <= 12. - Every car is distinct; the target order is fixed before the random initial order is chosen.
- The answer is a probability, rather than a count of arrangements or a yes/no answer for one supplied order.