# File: perm.py # Author: San Skulrattanakulchai def permutation(n): ''' Precondition: Assuming array A[0..n-1] is a permutation of [n], and k is some integer between 0 and n-1 inclusive. perm(k) will generate all permutation of the subarray A[k..n-1] but fixing A[0..k-1], and for each permutation (of A itself), prints it out. ''' def perm(k): if k == n: print(A) else: for i in range(k, n): A[i], A[k] = A[k], A[i] perm(k+1) A[i], A[k] = A[k], A[i] A = list(range(n)) perm(0) ###################### Testing ###################### permutation(4)