Sep-10-2018, 12:18 PM
Hello,
I need to implement a function that accepts an input of a list and return the sorted version of this list after generating all possible permutations
e.g.:
3, 7, 1
its permutations are:
1, 3, 7
1, 7, 3
3, 7, 1
3, 1, 7
7, 1, 3
7, 3, 1
I managed to write a recursive function to generate these permutations but I want this function to output (1, 3, 7) only how can I do this?
I need to implement a function that accepts an input of a list and return the sorted version of this list after generating all possible permutations
e.g.:
3, 7, 1
its permutations are:
1, 3, 7
1, 7, 3
3, 7, 1
3, 1, 7
7, 1, 3
7, 3, 1
I managed to write a recursive function to generate these permutations but I want this function to output (1, 3, 7) only how can I do this?
arr = [7, 1, 3] sortedList = arr def permutationSort(arr, flag, p, ): if len(arr) == len(p): print(p) #here I can see that I can get all permutations global sortedList if p < sortedList: sortedList = p for i in range(len(arr)): if flag[i] == 1: continue else: flag[i] = 1 p.append(arr[i]) permutationSort(arr, flag, p) p.pop() flag[i] = 0 permutationSort(arr, [0]*(len(arr)), []) print(sortedList) #this is not the correct answer