def permutations(a):
result = []
helper(a, result, 0, len(a))
return result
def helper(a, result, lo, hi):
if lo == hi:
print(a)
result.append(a)
else:
cursor = lo
for i in range(lo, hi):
a[cursor], a[i] = a[i], a[cursor]
helper(a, result, lo + 1, hi)
a[cursor], a[i] = a[i], a[cursor]
if __name__ == "__main__":
a = [1, 2, 3]
re = permutations(a)
print(re)
output:
I really can"t figure out why all the values in result are [1, 2, 2, 3]
.