I wrote a some functions to output the permutation of a list, I gave an input: [1], it's supposed to output [[1]], but my codes output: [[]], I've tried to print logs, looks like in the middle of the code run it did printout [[1]], but not sure why at the end it output [[]]? And how to fix it? Anybody can help? Thanks a lot!
def permute(nums):
result=[]
visited=[False]*len(nums)
nums=sorted(nums)
dfs(nums, visited, [], result)
return result
def dfs(nums, visited, tmp, result):
if len(tmp)==len(nums):
result.append(tmp)
print(result) ##here it shows correctly [[1]]
return
for i in range(len(nums)):
if visited[i]:
continue
if i>0 and tmp[i]==tmp[i-1] and not visited[i-1]:
continue
tmp.append(nums[i])
visited[i]=True
dfs(nums, visited, tmp, result)
visited[i]=False
tmp.pop()
a=[1]
result=permute(a)
print("------")
print(result)