Question: In Python 3 Write a function W(n) which for a given positive integer n returns a generator with all permutations of the set [0,1,2, ,n
In Python 3
Write a function W(n) which for a given positive integer n returns a generator with all permutations of the set [0,1,2, ,n -1 in which for alli 0,1,2.n-1, the number i is not at the ith position. E.g. For n = 3, there are 3! = 6 permutations [0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, O, (2, 0, 1), (2, 1, 0), And W(3) should return the generator with permutations (1, 2, 0) and (2, 0,1) In [4]: | # example how to create a list of all permutations: from itertools import permutations [perm for perm in permutations (range(3), 3) Out[4]: [(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0 Furthermore, to motivate the current problem, one may consider the following problem. There are Sn$ people waiting to enter a movie theater room with Sn sits. Every person has already purchased a ticket and is assigned a unique chair, enumerated from (0, 1, 2,... .n-1). People enter the room and choose chairs (one person - one sit). In how many ways can these people choose chairs such that **there is no person in his/her assigned chair**
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
