Write a python function to generate permuations of a list of given numbers


Topic: Write a python function to generate permuations of a list of given 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