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.