I'm having trouble solving this task:
Write a recursive function that inputs a positive integer n and outputs all n! permutations of {1, 2, . . . , n}. Do not use any of Sage’s permutation commands. Use a list to store the values and work with that list.
Sample Input: 3
Expected Output: (1,2,3), (1,3,2), (2,3,1),(2,1,3), (3,1,2), (3,2,1)
All the things that I have found online generates permutation of list not for an integer.
I thought of calculating my factorial will help determining my output length. I could not thing of how would I do this. Please help me out! Thank you !
I have tried this
def per(n):
a = []
for i in range(n):
for j in range(n-i):
a.append((i, j, n-i-j-1))
return a
per(3)
[(0, 0, 2), (0, 1, 1), (0, 2, 0), (1, 0, 1), (1, 1, 0), (2, 0, 0)]