0

Im trying to do permutation problem in leetcode with backtracking Algorithm, While Printing I got all the Possiblities but when i Trying to store those value in global variable I'm not allow to that

Ex:
AnswerIwant:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

when i print those value while recursion it is print 
Example:
[1, 2, 3]
[1, 3, 2]
[2, 3, 1]
[2, 1, 3]
[3, 1, 2]
[3, 2, 1]

When I store The Value In Global variable for outPut I got like This

[1, 2, 3]
[[1, 2, 3]]
[[1, 2]]
[1, 3, 2]
[[1, 3, 2], [1, 3, 2]]
[[1, 3], [1, 3]]
[[1], [1]]
[2, 3, 1]
[[2, 3, 1], [2, 3, 1], [2, 3, 1]]
[[2, 3], [2, 3], [2, 3]]
[2, 1, 3]
[[2, 1, 3], [2, 1, 3], [2, 1, 3], [2, 1, 3]]
[[2, 1], [2, 1], [2, 1], [2, 1]]
[[2], [2], [2], [2]]
[3, 1, 2]
[[3, 1, 2], [3, 1, 2], [3, 1, 2], [3, 1, 2], [3, 1, 2]]
[[3, 1], [3, 1], [3, 1], [3, 1], [3, 1]]
[3, 2, 1]
[[3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1]]
[[3, 2], [3, 2], [3, 2], [3, 2], [3, 2], [3, 2]]
[[3], [3], [3], [3], [3], [3]]

[[], [], [], [], [], []]

Here Is my code For the Above OutPut

nums = [1,2,3]
val=nums
answerIwant = []
numberOfValues=len(val)-1
answer=[]
def permutation(nums, bucket, index,sub):
    global answerIwant,numberOfValues
    for i in range(len(nums)):
        val=nums.pop(0)
        bucket.append(val)
        if len(bucket)-1==numberOfValues:
            print(bucket)
            answerIwant.append(bucket)
        permutation(nums, bucket, index,sub)
        nev=bucket.pop()
        nums.append(nev)
    print(answerIwant)
#[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
sub=[]
bucket = []

val=(permutation(nums, bucket, index,sub))
print(val)

Explain that problem i'm facing with symbol word Thank You

  • Is this what you are looking for? https://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list – Jalaj Varshney Jun 28 '21 at 11:18
  • I Already solved in another Method,I'm Trying to solve this with backtrackg for to learn backtracking i got all my possiblities but the problem is I cant strore those value in global variable I dont have any idea about thhat why it dont work – hussainPythonista Jun 28 '21 at 11:21

1 Answers1

0

You have to append the copy of the list during appening in the answerIwant list. So you should replace answerIwant.append(bucket) to answerIwant.append(bucket.copy()).

Refer this for more info. So your code could be this,

nums = [1,2,3]
val=nums
answerIwant = []
numberOfValues=len(val)-1
answer=[]
def permutation(nums, bucket, index,sub):
    global numberOfValues
    for i in range(len(nums)):
        val=nums.pop(0)
        bucket.append(val)
        if len(bucket)-1==numberOfValues:
            # print(bucket)
            answerIwant.append(bucket.copy())
        permutation(nums, bucket, index,sub)
        nev = bucket
        nev=bucket.pop()
        nums.append(nev)

#[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
sub=[]
bucket = []

permutation(nums, bucket, 0,sub)

print(answerIwant)
starboy_jb
  • 899
  • 1
  • 6
  • 13