'Python Permutation Backtracking - Why do I have to call list in this backtracking example?
In the code blow why do I need to call list() in the base case? If I don't call the list on current_perm, it just returns a list of empty lists.
permutations = []
# returns a list of its permutation
def permutation(nums, current_perm = []):
if len(nums) == 0:
# must call list on current_perm
# not sure why
permutations.append(list(current_perm))
else:
for idx, num in enumerate(nums):
current_perm.append(num)
# pass nums as nums without the current number
permutation(nums[:idx]+nums[idx+1:], current_perm)
# backtrack
current_perm.pop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
