Python permutation using backtracking

断了今生、忘了曾经 提交于 2019-12-12 04:40:42

问题


I'm just trying to learn permutation using backtracking. I've written the following code but it stops after first output.

def permutations(str_in, soFar):

    if len(str_in) != 0:
        for c in str_in:
            soFar.append(c)
            temp_str = str_in
            temp_str.remove(c)
            print temp_str, soFar
            permutations(temp_str, soFar)
    else:
        print soFar

inp = "ABCD"        
str_in = list(inp)
permutations(str_in, [])

This is the output I'm getting for this:

['B', 'C', 'D'] ['A']
['C', 'D'] ['A', 'B']
['D'] ['A', 'B', 'C']
[] ['A', 'B', 'C', 'D']
['A', 'B', 'C', 'D']

I'm sure this is something simple, but I'm not able to understand what mistake I'm making here.


回答1:


Here is Geeksforgeeks method, by Bhavya Jain, of permuting a string. The missing logic in your code is the recursive step on the sublists and a swapping behavior. Here is their visualization.

def permute(a, l, r):
    if l==r:
        print toString(a)
    else:
        for i in xrange(l,r+1):
            a[l], a[i] = a[i], a[l]
            permute(a, l+1, r)
            a[l], a[i] = a[i], a[l] # backtrack

# Driver program to test the above function
string = "ABC"
n = len(string)
a = list(string)
permute(a, 0, n-1)

Output

['A', 'B', 'C']
['A', 'C', 'B']
['B', 'A', 'C']
['B', 'C', 'A']
['C', 'B', 'A']
['C', 'A', 'B']



回答2:


I rewrote it again and with some print commands in between I was able to reach the desired output. But still not entirely sure of the way it's working. I think it's mainly how python modifies a list with each call to the function. I had to assign temporary lists twice to make sure when tracing back the original lists are not modified. Anyway the following code is working.

def permute(String, SoFar):
    TempString = list(String)
    TempSoFar = list(SoFar)
    #print TempString, TempSoFar
    if TempString != []:
        for c in String:
            TStr = list(TempString)
            TSF = list(TempSoFar)
            TStr.remove(c)
            TSF.append(c)
            #print String, TStr, TSF
            permute(TStr, TSF)
    else:
        print "\nOut: ", TempSoFar, "\n"

permute(list("ABCD"),list(""))

Second solution using strings rather than lists, as mentioned in my comment below.

def permute(String, SoFar):
    #print "New Func Call: ", "SoFar: ", SoFar,"String: ", String, "\n"
    if String != "":
        for c in String:
            TS = String.replace(c,"")
            TSF = SoFar+c
            permute(TS, TSF)
            #print "A Call Finished", "SoFar: ", SoFar,"String: ", String, "TSF: ", TSF, "TS: ", TS
    else:
        print SoFar

permute("ABC","")


来源:https://stackoverflow.com/questions/45763049/python-permutation-using-backtracking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!