Code
def permute(A, p, n):
if n == 1:
return [(A[p],)]
else:
B = permute(A, p+1, n-1)
return [insertHelper(x, i, A[p]) for i in range(n) for x in B]
def insertHelper(x, i, m):
x = list(x)
x.insert(i, m)
return tuple(x)
def main():
A = [3, 1, 2]
print('the permutation of A is: ', permute(A, 0, len(A)))
main()
Mathematical formula
![][1]
[1]: http://latex.codecogs.com/svg.latex?A_n=n!
![][2]
[2]: http://latex.codecogs.com/svg.latex?A_n^m=\frac{n!}{(n-m)!}