I am trying to generate all permutations for a given set. On each callback of my code, the contents of the array change for some reason, when I am using the append function. Can someone point me in the right direction?
class Solution(object):
def permute(self, nums):
res = []
self.generate_permutations(nums, res, 0, len(nums)-1)
return res
def generate_permutations(self, nums, res, l, r):
if l == r:
res.append(nums)
for i in range(l, r+1):
nums[i], nums[l] = nums[l], nums[i]
print('res', res)
self.generate_permutations(nums, res, l+1, r)
nums[l], nums[i] = nums[i], nums[l]
return res.appendis going to returnNoneand you never capture the recursive return value, butitertoolsis your friend, and you should learn that method to make permutations