I am writing a function to get all of the permutations for a given input:
e.g. Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
def permute(nums):
permutation_set = []
pointer_stand = 0
pointer_runner = 0
permutation_set.append(nums)
while pointer_stand < len(nums)-1:
while pointer_runner < len(nums)-1:
pointer_runner +=1
nums[pointer_stand], nums[pointer_runner] = nums[pointer_runner], nums[pointer_stand]
permutation_set.append(nums)
pointer_stand += 1
pointer_runner = pointer_stand
return permutation_set
I can't seem to see where I am going wrong, but the output I am getting is [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]
Any idea where the code is wrong? Because when I write it out, it should work! Thanks
P.S. I know the code above doesn't produce all of the permutations, I still need to 'rotate' the original input to get the rest of the permutations, but for now I am stuck swapping each index, as per my question above. Thanks