I am trying to make a recursive function which prints all possible permutations of a given set.
perm = [] # list to make permutations
S = {1,2,3,4} # set of elements which have not used for the permutation yet
def make_perm():
if not S:
print(perm)
else:
for x in S:
perm.append(x)
S.remove(x)
make_perm()
perm.pop()
S.add(x)
make_perm()
However, this program doesn't work: it outputs [1,2,3,4]
only.
What is the cause?
(Add) I want the program to output as follows.
> [1,2,3,4]
> [1,2,4,3]
> [1,3,2,4]
> [1,3,4,2]
︙
When it is complied with PyPy3(7.3.0), it outputs [1,2,3,4]
only.
But when it is complied with Python3(3.8.2), it outputs as follows.
> [1,2,3,4]
> [1,2,4,3]
> [1,3,2,4]
> [1,3,2,4]
> [1,3,4,2]
︙
Some outputs are duplicating. I am very confused that these outputs are incorrect and different :(.