Can anyone help me understand what's going on in this function? This code is from the book Learning Python 5th Edition* by Mark Lutz, Chapter 20, Comprehensions and Generations, 'Permutations: All possible combinations'...
def permute1(seq):
if not seq:
return [seq]
else:
res = []
for i in range(len(seq)):
rest = seq[:i] + seq[i+1:]
for x in permute1(rest):
res.append(seq[i:i+1]+x)
return res
It's supposed to generate all possible permutations from a sequence. The author explained what it does, but refused to explain what goes on under the hood. I tried to print the 'rest' and 'seq[i:i+1]+x' in hopes of tracing their outputs. But I'm still confused as to how the each permutation is generated.