Python passes the lists as a reference to a function. is the behavior any different when calling class function using self pointer? Why the finalResultList is not printing all the values and only empty brackets. Is it something to do with recursion?
Code:
class Solution:
def getAllPermutations(self,nums,index,resultList,finalResultList):
if(index == len(nums)):
finalResultList.append(resultList)
print(finalResultList)
return
for i in range(len(nums)):
if nums[i] not in resultList:
resultList.append(nums[i])
self.getAllPermutations(nums,index+1,resultList,finalResultList)
if(len(resultList) != 0):
resultList.pop(-1)
def permute(self, nums: List[int]) -> List[List[int]]:
resultList = []
finalResultList = []
self.getAllPermutations(nums,0,resultList,finalResultList)
print(finalResultList )
Output :
[[1, 2, 3]]
[[1, 3, 2], [1, 3, 2]]
[[2, 1, 3], [2, 1, 3], [2, 1, 3]]
[[2, 3, 1], [2, 3, 1], [2, 3, 1], [2, 3, 1]]
[[3, 1, 2], [3, 1, 2], [3, 1, 2], [3, 1, 2], [3, 1, 2]]
[[3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1]]
[[], [], [], [], [], []]
self.resultList?