Write a function to generate permutation of list of numbers


Topic: Write a function to generate permutation of list of numbers

Solution

def permute(nums):
    def backtrack(first = 0):
        if first == n:      
            output.append(nums[:])
        for i in range(first, n):
            nums[first], nums[i] = nums[i], nums[first]            
            backtrack(first + 1)
            nums[first], nums[i] = nums[i], nums[first]
 
        
    n = len(nums)
    output = []
    backtrack()
    return output
	
        



List all Python Programs